************************************************************/
*IMPORT DATA, CLEAN, INTERPOLATE GAPS, AND CORRECT ID CCODE*/
************************************************************/

*set directory to the folder containing data files
cd "C:\Users\treis\Dropbox\Dem Breakdown\CPS\New folder"

***************************************************************
*POLITY V DATA (http://www.systemicpeace.org/inscr/p5v2018.xls)
***************************************************************
import excel "http://www.systemicpeace.org/inscr/p5v2018.xls",  firstrow clear
xtset ccode year
replace ccode = 347 if country=="Kosovo"
*remove duplicate Yugoslavia 1991 entry
drop if cyear==3451991
*combine Yugoslavia pre 2003 with Serbia and Montenegro 2003-2006
replace ccode = 342 if country=="Yugoslavia"
replace ccode=342 if country=="Serbia and Montenegro"&year>2002&year<2007
replace ccode=342 if country=="Serbia"&year>2006
drop if country=="Serbia"&year==2006
replace ccode = 341 if country=="Montenegro"
*remove duplicate Ethiopia 1993 entry
drop if cyear==5291993
replace ccode=530 if country=="Ethiopia"
*consolidate Pakistan entries*
replace ccode=770 if country=="Pakistan"
*combine WG with Prussia and Germany with ccode = 255
drop if cyear==2601945
drop if cyear==2601990
replace ccode=255 if ccode==260
*combine USSR with Russia
drop if ccode==365&year==1922
replace ccode=365 if ccode==364
*use only up to 2018, last year with more than one case
replace polity2=. if year>2018

*make consistent cyear
drop cyear
gen cyear = ccode*10000 + year

*drop missing values
replace democ = . if democ<-10
replace autoc = . if autoc <-10
replace xrreg = . if xrreg <-10
replace xrcomp = . if xrcomp <-10
replace xropen = . if xropen <-10
replace xconst = . if xconst <-10
replace parreg = . if parreg <-10
replace parcomp = . if parcomp <-10
replace exrec = . if exrec <-10
replace exconst = . if exconst <-10
replace polcomp = . if polcomp<-10
replace polity2=. if polity<-10

xtset ccode year
by ccode: ipolate polity2 year, gen(pol3)
replace pol3=. if polity2==.&polity~=-77&polity~=-88
replace polity2=pol3

*I interpolate linearly to replace Polity's code for transitional or interregnum years (-88 and -77).
*I leave years of foreign occupation (polity = -66) as missing. Polity codes the interregnum years (polity=-77) as 0.
*This means that, for instance, during Afghanistan's civil war after 1991 the country's regime shot up to 0 from -8, only to fall back down
*to -7 when the Taliban took over. My code is not perfect--the -66 years are included in the interpolations, but then replaced by missing--but is very close to right.

*make marker for start of data--i.e. independence or entry to the dataset
gen polyear = year
replace polyear=. if polity==.
bysort ccode: egen minyear=min(polyear)
gen indep=0
replace indep=1 if year>minyear-1

*Make rescaled polity2 variable*
gen pol2norm = .
replace pol2norm = (5*polity2+50)/100 if polity2~=.
xtset ccode year
label var pol2norm "Polity2 on 0-1 scale"

*Make binary democracy variable
gen pol2more5 = .
replace pol2more5=1 if polity2>=6&polity2~=.
replace pol2more5=0 if polity2<6
label var pol2more5 "Polity2 >=6"

*all cases in which Polity2 falls below 6 having been above 6  
xtset ccode year
gen polbkd=.
replace polbkd=1 if l.pol2more5==1&pol2more5==0
replace polbkd=0 if l.pol2more5==1&pol2more5==1
label var polbkd "Polity2 falls from 6-10 to below 6"

gen polfact = 0
replace polfact=1 if parcomp==3
replace polfact=. if parcomp==.
label var parcomp "Polity2 rates participation as factionalized: parcomp"
xtset ccode year
gen lpolfact=l.polfact
gen lpolity2=l.polity2

save "breakdown cases.dta", replace

keep country year ccode cyear polity2 lpolity2 pol2norm pol2more5 polbkd indep parcomp polfact lpolfact
sort cyear
sleep 400
save "breakdown cases.dta", replace
sleep 400

keep ccode country year cyear polity2 indep parcomp polfact lpolity2
sort cyear
save "polshort.dta", replace

******************************************
*VDEM DATA 
******************************************
use "V-Dem-CY-Full+Others-v12.dta", clear
xtset country_id year
gen ccode=.
replace ccode = 100 if country_name=="Colombia"
replace ccode = 1001 if country_name=="Hong Kong"|country_name=="China: Hong Kong SAR"|country_name=="Hong Kong SAR"|country_name=="Hong Kong SAR, China"|country_name=="China, Hong Kong SAR"|country_name=="Hong Kong, China"
replace ccode = 1002 if country_name=="Puerto Rico"
replace ccode = 1005 if country_name=="Palestine"|country_name=="State of Palestine"|country_name=="Palestinian Territories"|country_name=="West Bank and Gaza Strip"|country_name=="West Bank and Gaza"|country_name=="Israeli-Occupied Territories and Palestinian Authority"|country_name=="Palestinian Authority"|country_name=="West Bank"|country_name=="Palestine, State of"
replace ccode = 101 if country_name=="Venezuela"|country_name=="Venezuela (Bolivarian Republic of)"|country_name=="Venezuela, RB"|country_name=="Venezuela, Bolivarian Republic of"
replace ccode = 110 if country_name=="Guyana"
replace ccode = 115 if country_name=="Suriname"
replace ccode = 130 if country_name=="Ecuador"
replace ccode = 135 if country_name=="Peru"
replace ccode = 140 if country_name=="Brazil"
replace ccode = 145 if country_name=="Bolivia"|country_name=="Bolivia (Plurinational State of)"|country_name=="Bolivia, Plurinational State of"
replace ccode = 150 if country_name=="Paraguay"
replace ccode = 155 if country_name=="Chile"
replace ccode = 160 if country_name=="Argentina"
replace ccode = 165 if country_name=="Uruguay"
replace ccode = 166 if country_name=="Faeroe Islands"
replace ccode = 2 if country_name=="United States of America"|country_name=="United States"|country_name=="USA"|country_name=="US"
replace ccode = 20 if country_name=="Canada"
replace ccode = 200 if country_name=="Great Britain"|country_name=="United Kingdom"|country_name=="UK"
replace ccode = 201 if country_name=="Channel Islands"
replace ccode = 202 if country_name=="Isle of Man"
replace ccode = 205 if country_name=="Ireland"
replace ccode = 210 if country_name=="Netherlands"
replace ccode = 211 if country_name=="Belgium"
replace ccode = 212 if country_name=="Luxembourg"
replace ccode = 220 if country_name=="France"
replace ccode = 221 if country_name=="Monaco"
replace ccode = 223 if country_name=="Liechtenstein"
replace ccode = 225 if country_name=="Switzerland"
replace ccode = 230 if country_name=="Spain"
replace ccode = 232 if country_name=="Andorra"
replace ccode = 235 if country_name=="Portugal"
replace ccode = 255 if country_name=="FRG/Germany"|country_name=="West Germany"|country_name=="Germany, West"|country_name=="Germany"|country_name=="West Germany (FRG)"|country_name=="German Federal Republic"|country_name=="Germany West"
replace ccode = 265 if country_name=="GDR"|country_name=="East Germany"|country_name=="Germany, East"|country_name=="East Germany (GDR)"|country_name=="Germany East"|country_name=="German Democratic Republic"
replace ccode = 290 if country_name=="Poland"
replace ccode = 305 if country_name=="Austria"
replace ccode = 31 if country_name=="Bahamas"
replace ccode = 31 if country_name=="Bahamas"|country_name=="Bahamas, The"|country_name=="The Bahamas"
replace ccode = 310 if country_name=="Hungary"
replace ccode = 316 if country_name=="Czech Rep."|country_name=="Czech Republic"|country_name=="Czechia"
replace ccode = 316 if country_name=="Czechoslovakia"
replace ccode = 317 if country_name=="Slovak Republic"|country_name=="Slovakia"|country_name=="Slovak Rep."
replace ccode = 32 if country_name=="Aruba"
replace ccode = 325 if country_name=="Italy"
replace ccode = 33 if country_name=="Bermuda"
replace ccode = 331 if country_name=="San Marino"
replace ccode = 338 if country_name=="Malta"
replace ccode = 339 if country_name=="Albania"
replace ccode = 341 if country_name=="Montenegro"
replace ccode = 342 if country_name=="Serbia and Montenegro"|country_name=="Serbia-Montenegro"
replace ccode = 342 if country_name=="Serbia"
replace ccode = 342 if country_name=="Yugoslavia"
replace ccode = 343 if country_name=="F.Y.R. Macedonia"|country_name=="Macedonia, FYR"|country_name=="FYR Macedonia"|country_name=="North Macedonia"|country_name=="Macedonia"|country_name=="The former Yugoslav Republic of Macedonia"|country_name=="Macedonia, the former Yugoslav Republic of"|country_name=="TFYR Macedonia"|country_name=="Macedonia (FYROM)"
replace ccode = 344 if country_name=="Croatia"
replace ccode = 346 if country_name=="Bosnia and Herzegovina"|country_name=="Bosnia"|country_name=="Bosnia-Herz"|country_name=="Bosnia-Herzegovina"|country_name=="Bosnia & Herzegovina"
replace ccode = 347 if country_name=="Kosovo"
replace ccode = 349 if country_name=="Slovenia"
replace ccode = 350 if country_name=="Greece"
replace ccode = 352 if country_name=="Cyprus"|country_name=="Cyprus (Greek)"
replace ccode = 355 if country_name=="Bulgaria"
replace ccode = 359 if country_name=="Moldova"|country_name=="Republic of Moldova"|country_name=="Moldova, Republic of"
replace ccode = 360 if country_name=="Romania"
replace ccode = 365 if country_name=="Russia"|country_name=="Russian Federation"|country_name=="Russia/USSR"|country_name=="USSR"|country_name=="Soviet Union"
replace ccode = 366 if country_name=="Estonia"
replace ccode = 367 if country_name=="Latvia"
replace ccode = 368 if country_name=="Lithuania"
replace ccode = 369 if country_name=="Ukraine"
replace ccode = 370 if country_name=="Belarus"
replace ccode = 371 if country_name=="Armenia"
replace ccode = 372 if country_name=="Georgia"
replace ccode = 373 if country_name=="Azerbaijan"
replace ccode = 375 if country_name=="Finland"
replace ccode = 380 if country_name=="Sweden"
replace ccode = 385 if country_name=="Norway"
replace ccode = 390 if country_name=="Denmark"
replace ccode = 395 if country_name=="Iceland"
replace ccode = 396 if country_name=="Greenland"
replace ccode = 40 if country_name=="Cuba"
replace ccode = 402 if country_name=="C. Verde Is."|country_name=="Cabo Verde"|country_name=="Cape Verde"
replace ccode = 403 if country_name=="Sao Tome and Principe"|country_name=="São Tomé and Príncipe"|country_name=="Sao Tome"|country_name=="S. Tomé & Principe"
replace ccode = 404 if country_name=="Guinea-Bissau"|country_name=="Guinea Bissau"
replace ccode = 41 if country_name=="Haiti"
replace ccode = 411 if country_name=="Eq. Guinea"|country_name=="Equatorial Guinea"
replace ccode = 42 if country_name=="Dom. Rep."|country_name=="Dominican Rep."|country_name=="Dominican Republic"|country_name=="Dominican Rep"
replace ccode = 420 if country_name=="Gambia"|country_name=="Gambia, The"|country_name=="The Gambia"
replace ccode = 432 if country_name=="Mali"
replace ccode = 433 if country_name=="Senegal"
replace ccode = 434 if country_name=="Benin"
replace ccode = 435 if country_name=="Mauritania"
replace ccode = 436 if country_name=="Niger"
replace ccode = 437 if country_name=="C?te d'Ivoire"|country_name=="Cote d`Ivoire"|country_name=="Côte d´Ivoire"|country_name=="Côte d’Ivoire"|country_name=="Côte d'Ivoire"|country_name=="Cote d'Ivoire"|country_name=="Ivory Coast (Cote d'Ivoire)"|country_name=="Ivory Coast"|country_name=="Côte d'Ivoire"|country_name=="Côte D'Ivoire"|country_name=="Cote D'Ivoire"
replace ccode = 438 if country_name=="Guinea"
replace ccode = 439 if country_name=="Burkina Faso"
replace ccode = 450 if country_name=="Liberia"
replace ccode = 451 if country_name=="Sierra Leone"
replace ccode = 452 if country_name=="Ghana"
replace ccode = 461 if country_name=="Togo"
replace ccode = 471 if country_name=="Cameroon"
replace ccode = 475 if country_name=="Nigeria"
replace ccode = 481 if country_name=="Gabon"
replace ccode = 482 if country_name=="Cent. Af. Rep."
replace ccode = 482 if country_name=="Central Afr. Rep."|country_name=="Cen African Rep"|country_name=="Central African Rep."
replace ccode = 482 if country_name=="Central African Republic"
replace ccode = 483 if country_name=="Chad"
replace ccode = 484 if country_name=="Congo (Brazzaville)"
replace ccode = 484 if country_name=="Congo Brazzaville"|country_name=="Congo-Brz"
replace ccode = 484 if country_name=="Congo Republic"
replace ccode = 484 if country_name=="Congo"
replace ccode = 484 if country_name=="Congo, Rep. Of"
replace ccode = 484 if country_name=="Congo, Rep."|country_name=="Congo-Brazzaville"|country_name=="Republic of Congo"|country_name=="Congo, Republic"|country_name=="Republic of the Congo"
replace ccode = 484 if country_name=="Congo, Republic of"|country_name=="Congo, Republic of (Brazzaville)"
replace ccode = 484 if country_name=="Congo, Republicof"|country_name=="Congo (Rep.)"
replace ccode = 490 if country_name=="Congo (Democratic Republic of the)"|country_name=="Congo, the Democratic Republic of the"|country_name=="Congo (Dem. Rep.)"|country_name==" Congo, D.R."|country_name=="Congo Kinshasa"
replace ccode = 490 if country_name=="Congo (DRC)"|country_name=="Congo, Democratic Republic of (Kinshasa)"|country_name=="Zaire (Democ Republic Congo)"|country_name=="Congo/Zaire"
replace ccode = 490 if country_name=="Congo (Kinshasa)"|country_name=="Congo-Kinshasa"
replace ccode = 490 if country_name=="Congo, Dem. R."
replace ccode = 490 if country_name=="Congo, Dem. Rep."|country_name=="Congo, Democratic Republic"|country_name=="Democratic Republic of Congo"|country_name=="Zaire"
replace ccode = 490 if country_name=="Congo, Democratic Republic of"|country_name=="Congo, Dem. Rep."|country_name=="D.R. of the Congo"
replace ccode = 490 if country_name=="Democratic Republic of the Congo"
replace ccode = 500 if country_name=="Uganda"
replace ccode = 501 if country_name=="Kenya"
replace ccode = 51 if country_name=="Jamaica"
replace ccode = 510 if country_name=="Tanzania (United Republic of)"|country_name=="U.R. of Tanzania: Mainland"|country_name=="Tanzania, United Republic of"
replace ccode = 510 if country_name=="Tanzania"|country_name=="United Republic of Tanzania"
replace ccode = 516 if country_name=="Burundi"
replace ccode = 517 if country_name=="Rwanda"
replace ccode = 52 if country_name=="Trinidad & Tob."
replace ccode = 52 if country_name=="Trinidad & Tobago"
replace ccode = 52 if country_name=="Trinidad and Tobago"
replace ccode = 52 if country_name=="Trinidad-Tobago"
replace ccode = 520 if country_name=="Somalia"
replace ccode = 522 if country_name=="Djibouti"
replace ccode = 53 if country_name=="Barbados"
replace ccode = 530 if country_name=="Ethiopia"
replace ccode = 531 if country_name=="Eritrea"
replace ccode = 54 if country_name=="Dominica"
replace ccode = 540 if country_name=="Angola"
replace ccode = 541 if country_name=="Mozambique"
replace ccode = 55 if country_name=="Grenada"
replace ccode = 551 if country_name=="Zambia"
replace ccode = 552 if country_name=="Zimbabwe"|country_name=="Rhodesia"
replace ccode = 553 if country_name=="Malawi"
replace ccode = 56 if country_name=="Saint Lucia"
replace ccode = 56 if country_name=="St. Lucia"
replace ccode = 560 if country_name=="S. Africa"
replace ccode = 560 if country_name=="South Africa"
replace ccode = 565 if country_name=="Namibia"
replace ccode = 57 if country_name=="Saint Vincent and the Grenadines"|country_name=="St Vincent"
replace ccode = 57 if country_name=="St. Vincent & Grenadines"
replace ccode = 57 if country_name=="St. Vincent and Grenadines"
replace ccode = 57 if country_name=="St. Vincent and the Grenadines"
replace ccode = 570 if country_name=="Lesotho"
replace ccode = 571 if country_name=="Botswana"
replace ccode = 572 if country_name=="Swaziland"|country_name=="Eswatini"
replace ccode = 58 if country_name=="Antigua and Barbuda"|country_name=="Antigua & Barbuda"
replace ccode = 580 if country_name=="Madagascar"
replace ccode = 581 if country_name=="Comoro Is."
replace ccode = 581 if country_name=="Comoros"
replace ccode = 59 if country_name=="Cayman Islands"
replace ccode = 590 if country_name=="Mauritius"
replace ccode = 591 if country_name=="Seychelles"
replace ccode = 60 if country_name=="Saint Kitts & Nevis"|country_name=="Saint Kitts and Nevis"|country_name=="St Kitts"
replace ccode = 60 if country_name=="St. Kitts and Nevis"
replace ccode = 60 if country_name=="St.Kitts & Nevis"
replace ccode = 600 if country_name=="Morocco"
replace ccode = 61 if country_name=="Curacao"
replace ccode = 615 if country_name=="Algeria"
replace ccode = 616 if country_name=="Tunisia"
replace ccode = 62 if country_name=="Turks and Caicos Islands"
replace ccode = 620 if country_name=="Libya"
replace ccode = 625 if country_name=="Sudan"|country_name=="Sudan (Former)"
replace ccode = 626 if country_name=="South Sudan"
replace ccode = 63 if country_name=="Virgin Islands (U.S.)"
replace ccode = 630 if country_name=="Iran (Islamic Republic of)"|country_name=="Iran, Islamic Republic of"
replace ccode = 630 if country_name=="Iran"|country_name=="Iran (I.R.)"
replace ccode = 630 if country_name=="Iran"
replace ccode = 630 if country_name=="Iran, Islamic Rep."|country_name=="Islamic Republic of Iran"
replace ccode = 640 if country_name=="Turkey"
replace ccode = 645 if country_name=="Iraq"
replace ccode = 651 if country_name=="Egypt"
replace ccode = 651 if country_name=="Egypt"
replace ccode = 651 if country_name=="Egypt, Arab Rep."
replace ccode = 652 if country_name=="Syria"
replace ccode = 652 if country_name=="Syria"
replace ccode = 652 if country_name=="Syrian Arab Republic"
replace ccode = 660 if country_name=="Lebanon"
replace ccode = 663 if country_name=="Jordan"
replace ccode = 666 if country_name=="Israel"|country_name=="Israel and Occupied Territories**"|country_name=="Israel in pre-1967 borders"
replace ccode = 670 if country_name=="Saudi Arabia"
replace ccode = 678 if country_name=="Yemen Arab Republic"|country_name=="Yemen (A.R.)"|country_name=="North Yemen"|country_name=="Yemen (AR)"|country_name=="Yemen North"
replace ccode = 679 if country_name=="Yemen, Rep."|country_name=="Yemen"|country_name=="Yemen, Republic of"
replace ccode = 680 if country_name=="Yemen (PDR)"|country_name=="South Yemen"|country_name=="Yemen, South"|country_name=="SouthYemen"|country_name=="Yemen People's Republic"|country_name=="Yemen  (PDR)"|country_name=="Yemen South"
replace ccode = 690 if country_name=="Kuwait"
replace ccode = 692 if country_name=="Bahrain"
replace ccode = 694 if country_name=="Qatar"
replace ccode = 696 if country_name=="Unit. Arab Em."
replace ccode = 696 if country_name=="United Arab Emirates"|country_name=="UAE"|country_name=="United Arab Emirates#"
replace ccode = 698 if country_name=="Oman"
replace ccode = 70 if country_name=="Mexico"
replace ccode = 700 if country_name=="Afghanistan"
replace ccode = 701 if country_name=="Turkmenistan"
replace ccode = 702 if country_name=="Tajikistan"
replace ccode = 703 if country_name=="Kyrgyz Republic"
replace ccode = 703 if country_name=="Kyrgyzstan"
replace ccode = 703 if country_name=="Kyrgyzstan"
replace ccode = 704 if country_name=="Uzbekistan"
replace ccode = 705 if country_name=="Kazakhstan"
replace ccode = 710 if country_name=="China (PRC)"
replace ccode = 710 if country_name=="China"
replace ccode = 710 if country_name=="China, P. R."
replace ccode = 710 if country_name=="PRC"
replace ccode = 711 if country_name=="China: Macao SAR"|country_name=="Macao, China"
replace ccode = 711 if country_name=="Macao SAR, China"|country_name=="China, Macao SAR"
replace ccode = 711 if country_name=="Macao"|country_name=="Macau"
replace ccode = 712 if country_name=="Mongolia"
replace ccode = 713 if country_name=="Taiwan (ROC)"|country_name=="Taiwan Province of China"|country_name=="Taiwan, China"|country_name=="Taiwan, Province of China"
replace ccode = 713 if country_name=="Taiwan"
replace ccode = 713 if country_name=="Taiwan"
replace ccode = 731 if country_name=="Korea (North)"|country_name=="Korea North"|country_name=="Korea, Democratic People's Republic of"
replace ccode = 731 if country_name=="Korea, Dem. Rep."|country_name=="Democratic People's Republic of Korea"|country_name=="D.P.R. Korea"
replace ccode = 731 if country_name=="North Korea"|country_name=="Korea, North"
replace ccode = 731 if country_name=="PRK"|country_name=="North Korea (Democratc People's Republic of Korea)"
replace ccode = 732 if country_name=="Korea (South)"|country_name=="Korea South"
replace ccode = 732 if country_name=="Korea, Rep."|country_name=="Korea"|country_name=="Korea (Rep.)"
replace ccode = 732 if country_name=="Korea, Republic of"
replace ccode = 732 if country_name=="Korea, South"
replace ccode = 732 if country_name=="ROK"
replace ccode = 732 if country_name=="South Korea"|country_name=="Korea, South"|country_name=="Republic of Korea"|country_name=="South Korea (Republic of Korea)"
replace ccode = 740 if country_name=="Japan"
replace ccode = 750 if country_name=="India"
replace ccode = 760 if country_name=="Bhutan"
replace ccode = 770 if country_name=="Pakistan"
replace ccode = 771 if country_name=="Bangladesh"
replace ccode = 775 if country_name=="Burma (Myanmar)"
replace ccode = 775 if country_name=="Burma"
replace ccode = 775 if country_name=="Myanmar"|country_name=="Myanmar (Burma)"
replace ccode = 780 if country_name=="Sri Lanka"
replace ccode = 781 if country_name=="Maldives"
replace ccode = 790 if country_name=="Nepal"
replace ccode = 80 if country_name=="Belize"
replace ccode = 800 if country_name=="Thailand"
replace ccode = 811 if country_name=="Cambodia"
replace ccode = 812 if country_name=="Lao PDR"|country_name=="Lao P.D.R."
replace ccode = 812 if country_name=="Lao People's Democratic Republic"
replace ccode = 812 if country_name=="Lao"|country_name=="Lao People's DR"
replace ccode = 812 if country_name=="Laos"
replace ccode = 812 if country_name=="Laos"
replace ccode = 817 if country_name=="Vietnam, South"|country_name=="South Vietnam"|country_name=="Republic of Vietnam"
replace ccode = 818 if country_name=="Viet Nam"|country_name=="Vietnam, Socialist Republic of"
replace ccode = 818 if country_name=="Vietnam"|country_name=="Vietnam North"
replace ccode = 820 if country_name=="Malaysia"
replace ccode = 830 if country_name=="Singapore"
replace ccode = 835 if country_name=="Brunei Darussalam"
replace ccode = 835 if country_name=="Brunei"
replace ccode = 835 if country_name=="Brunei"
replace ccode = 840 if country_name=="Philippines"|country_name=="Philippines +"
replace ccode = 850 if country_name=="Indonesia"
replace ccode = 860 if country_name=="East Timor"|country_name=="East Timor (Timor L'este)"
replace ccode = 860 if country_name=="Timor-Leste"|country_name=="Timor Leste"
replace ccode = 90 if country_name=="Guatemala"
replace ccode = 900 if country_name=="Australia"
replace ccode = 91 if country_name=="Honduras"
replace ccode = 910 if country_name=="P.N. Guinea"
replace ccode = 910 if country_name=="Papua New Guinea"|country_name=="P.N. Guinea"|country_name=="P. N. Guinea"
replace ccode = 92 if country_name=="El Salvador"
replace ccode = 920 if country_name=="New Zealand"
replace ccode = 93 if country_name=="Nicaragua"
replace ccode = 935 if country_name=="Vanuatu"
replace ccode = 94 if country_name=="Costa Rica"
replace ccode = 940 if country_name=="Solomon Is."
replace ccode = 940 if country_name=="Solomon Islands"
replace ccode = 941 if country_name=="Northern Mariana Islands"
replace ccode = 946 if country_name=="Kiribati"
replace ccode = 947 if country_name=="Tuvalu"
replace ccode = 95 if country_name=="Panama"
replace ccode = 950 if country_name=="Fiji"
replace ccode = 951 if country_name=="French Polynesia"
replace ccode = 952 if country_name=="New Caledonia"
replace ccode = 955 if country_name=="Tonga"
replace ccode = 983 if country_name=="Marshall Islands"
replace ccode = 986 if country_name=="Palau"
replace ccode = 987 if country_name=="Micronesia (Federated States of)"|country_name=="Micronesia (FS of)"|country_name=="Micronesia, Federated States of"
replace ccode = 987 if country_name=="Micronesia"
replace ccode = 987 if country_name=="Micronesia, Fed. Sts."
replace ccode = 990 if country_name=="Samoa"|country_name=="W. Samoa"|country_name=="Western Samoa"
replace ccode = 991 if country_name=="Guam"
replace ccode = 992 if country_name=="Nauru"
replace ccode = 993 if country_name=="Niue"
replace ccode = 994 if country_name=="Cook Islands"
replace ccode = 995 if country_name=="American Samoa"
replace ccode=35 if country_name=="Anguilla"
replace ccode=36 if country_name=="Curaçao"
replace ccode=37 if country_name=="Montserrat"
replace ccode=38 if country_name=="Sint Maarten (Dutch part)"
replace ccode=39 if country_name=="British Virgin Islands"
replace ccode=521 if country_name=="Somaliland"|country_name=="Somaliland Region"
replace ccode=353 if country_name=="Northern Cyprus"
replace ccode=775 if country_name=="Burma/Myanmar"
replace ccode=511 if country_name=="Zanzibar"
replace ccode=1005 if country_name=="Palestine/West Bank"
replace ccode=1006 if country_name=="Palestine/Gaza"
drop if ccode==.
gen cyear=ccode*10000+year
sort cyear

gen vdemelec=0
replace vdemelec=1 if v2x_regime==2
replace vdemelec=. if v2x_regime==.
label var vdemelec "elec democracy: v2x_regime>=2"

gen vdemlib = 0
replace vdemlib=1 if v2x_regime==3
replace vdemlib=. if v2x_regime==.
label var vdemlib "liberal democracy: v2x_regime=3"
bys year: egen mvdemlib = mean(vdemlib)

gen vdemdem = 0
replace vdemdem=1 if v2x_regime==3|v2x_regime==2
replace vdemdem=. if v2x_regime==.
label var vdemdem "vdem elec or lib democracy: v2x_regime=2 or 3"
bys year: egen mvdemdem = mean(vdemdem)

xtset country_id year
gen lv2caviol = l.v2caviol
gen lv2cacamps = l.v2cacamps
gen lv2xcivlib = l.v2x_civlib
bys year: egen mvpoly = mean(v2x_polyarchy)
bys year: egen mvlib = mean(v2x_libdem)

keep country_name year cyear ccode e_pop v2x_regime_amb lv2caviol v2caviol lv2cacamps v2cacamps lv2xcivlib v2caautmob v2x_polyarchy v2x_libdem v2x_liberal e_v2x_polyarchy_5C e_v2x_libdem_5C v2x_regime v2elmulpar_osp v2elfrfair_osp v2cltrnslw_osp v2cltrnslw_osp_codelow v2cltrnslw_osp_codehigh v2clacjstm_osp v2clacjstm_osp_codehigh v2clacjstm_osp_codelow v2clacjstw_osp v2clacjstw_osp_codehigh v2clacjstw_osp_codelow vdemlib vdemelec vdemdem v2x_regime e_regionpol v2cacamps_ord v2smpolsoc_ord mvdemdem v2x_civlib e_pt_coup e_miinteco e_coups v2elmulpar_osp_codehigh v2elfrfair_osp_codehigh v2x_polyarchy_codehigh v2x_liberal_codehigh
sort cyear
save "vdem12wk.dta", replace
keep  year cyear ccode vdemdem vdemlib
sort cyear
save "vdemshort.dta", replace


****************************************************************************
*VDEM Autocratization Episodes (Luhrman and Lindberg 2019)
****************************************************************************
import excel "llautocratization.xlsx", sheet("Sheet1") firstrow clear
gen ccode=.
replace ccode = 100 if country=="Colombia"
replace ccode = 1001 if country=="Hong Kong"|country=="China: Hong Kong SAR"|country=="Hong Kong SAR"|country=="Hong Kong SAR, China"|country=="China, Hong Kong SAR"|country=="Hong Kong, China"
replace ccode = 1002 if country=="Puerto Rico"
replace ccode = 1005 if country=="Palestine"|country=="State of Palestine"|country=="Palestinian Territories"|country=="West Bank and Gaza Strip"|country=="West Bank and Gaza"|country=="Israeli-Occupied Territories and Palestinian Authority"|country=="Palestinian Authority"|country=="West Bank"|country=="Palestine, State of"
replace ccode = 101 if country=="Venezuela"|country=="Venezuela (Bolivarian Republic of)"|country=="Venezuela, RB"|country=="Venezuela, Bolivarian Republic of"
replace ccode = 110 if country=="Guyana"
replace ccode = 115 if country=="Suriname"
replace ccode = 130 if country=="Ecuador"
replace ccode = 135 if country=="Peru"
replace ccode = 140 if country=="Brazil"
replace ccode = 145 if country=="Bolivia"|country=="Bolivia (Plurinational State of)"|country=="Bolivia, Plurinational State of"
replace ccode = 150 if country=="Paraguay"
replace ccode = 155 if country=="Chile"
replace ccode = 160 if country=="Argentina"
replace ccode = 165 if country=="Uruguay"
replace ccode = 166 if country=="Faeroe Islands"
replace ccode = 2 if country=="United States of America"|country=="United States"|country=="USA"|country=="US"
replace ccode = 20 if country=="Canada"
replace ccode = 200 if country=="Great Britain"|country=="United Kingdom"|country=="UK"
replace ccode = 201 if country=="Channel Islands"
replace ccode = 202 if country=="Isle of Man"
replace ccode = 205 if country=="Ireland"
replace ccode = 210 if country=="Netherlands"
replace ccode = 211 if country=="Belgium"
replace ccode = 212 if country=="Luxembourg"
replace ccode = 220 if country=="France"
replace ccode = 221 if country=="Monaco"
replace ccode = 223 if country=="Liechtenstein"
replace ccode = 225 if country=="Switzerland"
replace ccode = 230 if country=="Spain"
replace ccode = 232 if country=="Andorra"
replace ccode = 235 if country=="Portugal"
replace ccode = 255 if country=="FRG/Germany"|country=="Germany, W. "|country=="Germany, West"|country=="Germany"|country=="West Germany (FRG)"|country=="German Federal Republic"|country=="Germany West"
replace ccode = 265 if country=="GDR"|country=="Germany, E. "|country=="Germany, East"|country=="East Germany (GDR)"|country=="Germany East"|country=="German Democratic Republic"
replace ccode = 290 if country=="Poland"
replace ccode = 305 if country=="Austria"
replace ccode = 31 if country=="Bahamas"
replace ccode = 31 if country=="Bahamas"|country=="Bahamas, The"|country=="The Bahamas"
replace ccode = 310 if country=="Hungary"
replace ccode = 316 if country=="Czech Rep."|country=="Czech Republic"|country=="Czechia"
replace ccode = 316 if country=="Czechoslovakia"
replace ccode = 317 if country=="Slovak Republic"|country=="Slovakia"|country=="Slovak Rep."
replace ccode = 32 if country=="Aruba"
replace ccode = 325 if country=="Italy"
replace ccode = 33 if country=="Bermuda"
replace ccode = 331 if country=="San Marino"
replace ccode = 338 if country=="Malta"
replace ccode = 339 if country=="Albania"
replace ccode = 341 if country=="Montenegro"
replace ccode = 342 if country=="Serbia and Montenegro"|country=="Serbia-Montenegro"
replace ccode = 342 if country=="Serbia"
replace ccode = 342 if country=="Yugoslavia"
replace ccode = 343 if country=="F.Y.R. Macedonia"|country=="Macedonia, FYR"|country=="FYR Macedonia"|country=="TFYR of Macedonia"|country=="Macedonia"|country=="The former Yugoslav Republic of Macedonia"|country=="Macedonia, the former Yugoslav Republic of"|country=="TFYR Macedonia"|country=="North Macedonia"
replace ccode = 344 if country=="Croatia"
replace ccode = 346 if country=="Bosnia and Herzegovina"|country=="Bosnia"|country=="Bosnia-Herz"|country=="Bosnia-Herzegovina"|country=="Bosnia & Herzegovina"
replace ccode = 347 if country=="Kosovo"
replace ccode = 349 if country=="Slovenia"
replace ccode = 350 if country=="Greece"
replace ccode = 352 if country=="Cyprus"|country=="Cyprus (Greek)"
replace ccode = 355 if country=="Bulgaria"
replace ccode = 359 if country=="Moldova"|country=="Republic of Moldova"|country=="Moldova, Republic of"
replace ccode = 360 if country=="Romania"
replace ccode = 365 if country=="Russia"|country=="Russian Federation"|country=="Russia/USSR"|country=="USSR"|country=="Soviet Union"
replace ccode = 366 if country=="Estonia"
replace ccode = 367 if country=="Latvia"
replace ccode = 368 if country=="Lithuania"
replace ccode = 369 if country=="Ukraine"
replace ccode = 370 if country=="Belarus"
replace ccode = 371 if country=="Armenia"
replace ccode = 372 if country=="Georgia"
replace ccode = 373 if country=="Azerbaijan"
replace ccode = 375 if country=="Finland"
replace ccode = 380 if country=="Sweden"
replace ccode = 385 if country=="Norway"
replace ccode = 390 if country=="Denmark"
replace ccode = 395 if country=="Iceland"
replace ccode = 396 if country=="Greenland"
replace ccode = 40 if country=="Cuba"
replace ccode = 402 if country=="C. Verde Is."|country=="Cabo Verde"|country=="Cape Verde"
replace ccode = 403 if country=="Sao Tome and Principe"|country=="São Tomé and Príncipe"|country=="Sao Tome"|country=="S. Tomé & Principe"
replace ccode = 404 if country=="Guinea-Bissau"|country=="Guinea Bissau"
replace ccode = 41 if country=="Haiti"
replace ccode = 411 if country=="Eq. Guinea"|country=="Equatorial Guinea"
replace ccode = 42 if country=="Dom. Rep."|country=="Dominican Rep."|country=="Dominican Republic"|country=="Dominican Rep"
replace ccode = 420 if country=="Gambia"|country=="Gambia, The"|country=="The Gambia"
replace ccode = 432 if country=="Mali"
replace ccode = 433 if country=="Senegal"
replace ccode = 434 if country=="Benin"
replace ccode = 435 if country=="Mauritania"
replace ccode = 436 if country=="Niger"
replace ccode = 437 if country=="C?te d'Ivoire"|country=="Cote d`Ivoire"|country=="Côte d´Ivoire"|country=="Côte d’Ivoire"|country=="Côte d'Ivoire"|country=="Cote d'Ivoire"|country=="Ivory Coast (Cote d'Ivoire)"|country=="Ivory Coast"|country=="Côte d'Ivoire"|country=="Côte D'Ivoire"|country=="Cote D'Ivoire"
replace ccode = 438 if country=="Guinea"
replace ccode = 439 if country=="Burkina Faso"
replace ccode = 450 if country=="Liberia"
replace ccode = 451 if country=="Sierra Leone"
replace ccode = 452 if country=="Ghana"
replace ccode = 461 if country=="Togo"
replace ccode = 471 if country=="Cameroon"
replace ccode = 475 if country=="Nigeria"
replace ccode = 481 if country=="Gabon"
replace ccode = 482 if country=="Cent. Af. Rep."
replace ccode = 482 if country=="Central Afr. Rep."|country=="Cen African Rep"|country=="Central African Rep."
replace ccode = 482 if country=="Central African Republic"
replace ccode = 483 if country=="Chad"
replace ccode = 484 if country=="Congo (Brazzaville)"
replace ccode = 484 if country=="Congo Brazzaville"|country=="Congo-Brz"
replace ccode = 484 if country=="Congo Republic"
replace ccode = 484 if country=="Congo"
replace ccode = 484 if country=="Congo, Rep. Of"
replace ccode = 484 if country=="Congo, Rep."|country=="Congo-Brazzaville"|country=="Republic of Congo"|country=="Congo, Republic"|country=="Republic of the Congo"
replace ccode = 484 if country=="Congo, Republic of"|country=="Congo, Republic of (Brazzaville)"
replace ccode = 484 if country=="Congo, Republicof"|country=="Congo (Rep.)"
replace ccode = 490 if country=="Congo (Democratic Republic of the)"|country=="Congo, the Democratic Republic of the"|country=="Congo (Dem. Rep.)"|country==" Congo, D.R."|country=="Congo Kinshasa"
replace ccode = 490 if country=="Congo (DRC)"|country=="Congo, Democratic Republic of (Kinshasa)"|country=="Zaire (Democ Republic Congo)"|country=="Congo/Zaire"
replace ccode = 490 if country=="Congo (Kinshasa)"|country=="Congo-Kinshasa"
replace ccode = 490 if country=="Congo, Dem. R."
replace ccode = 490 if country=="Congo, Dem. Rep."|country=="Congo, Democratic Republic"|country=="Democratic Republic of Congo"|country=="Zaire"
replace ccode = 490 if country=="Congo, Democratic Republic of"|country=="Congo, Dem. Rep."|country=="D.R. of the Congo"
replace ccode = 490 if country=="Democratic Republic of the Congo"
replace ccode = 500 if country=="Uganda"
replace ccode = 501 if country=="Kenya"
replace ccode = 51 if country=="Jamaica"
replace ccode = 510 if country=="Tanzania (United Republic of)"|country=="U.R. of Tanzania: Mainland"|country=="Tanzania, United Republic of"
replace ccode = 510 if country=="Tanzania"|country=="United Republic of Tanzania"
replace ccode = 516 if country=="Burundi"
replace ccode = 517 if country=="Rwanda"
replace ccode = 52 if country=="Trinidad & Tob."
replace ccode = 52 if country=="Trinidad & Tobago"
replace ccode = 52 if country=="Trinidad and Tobago"
replace ccode = 52 if country=="Trinidad-Tobago"
replace ccode = 520 if country=="Somalia"
replace ccode = 522 if country=="Djibouti"
replace ccode = 53 if country=="Barbados"
replace ccode = 530 if country=="Ethiopia"
replace ccode = 531 if country=="Eritrea"
replace ccode = 54 if country=="Dominica"
replace ccode = 540 if country=="Angola"
replace ccode = 541 if country=="Mozambique"
replace ccode = 55 if country=="Grenada"
replace ccode = 551 if country=="Zambia"
replace ccode = 552 if country=="Zimbabwe"|country=="Rhodesia"
replace ccode = 553 if country=="Malawi"
replace ccode = 56 if country=="Saint Lucia"
replace ccode = 56 if country=="St. Lucia"
replace ccode = 560 if country=="S. Africa"
replace ccode = 560 if country=="South Africa"
replace ccode = 565 if country=="Namibia"
replace ccode = 57 if country=="Saint Vincent and the Grenadines"|country=="St Vincent"
replace ccode = 57 if country=="St. Vincent & Grenadines"
replace ccode = 57 if country=="St. Vincent and Grenadines"
replace ccode = 57 if country=="St. Vincent and the Grenadines"
replace ccode = 570 if country=="Lesotho"
replace ccode = 571 if country=="Botswana"
replace ccode = 572 if country=="Swaziland"|country=="Eswatini"
replace ccode = 58 if country=="Antigua and Barbuda"|country=="Antigua & Barbuda"
replace ccode = 580 if country=="Madagascar"
replace ccode = 581 if country=="Comoro Is."
replace ccode = 581 if country=="Comoros"
replace ccode = 59 if country=="Cayman Islands"
replace ccode = 590 if country=="Mauritius"
replace ccode = 591 if country=="Seychelles"
replace ccode = 60 if country=="Saint Kitts & Nevis"|country=="Saint Kitts and Nevis"|country=="St Kitts"
replace ccode = 60 if country=="St. Kitts and Nevis"
replace ccode = 60 if country=="St.Kitts & Nevis"
replace ccode = 600 if country=="Morocco"
replace ccode = 61 if country=="Curacao"
replace ccode = 615 if country=="Algeria"
replace ccode = 616 if country=="Tunisia"
replace ccode = 62 if country=="Turks and Caicos Islands"
replace ccode = 620 if country=="Libya"
replace ccode = 625 if country=="Sudan"|country=="Sudan (Former)"
replace ccode = 626 if country=="South Sudan"
replace ccode = 63 if country=="Virgin Islands (U.S.)"
replace ccode = 630 if country=="Iran (Islamic Republic of)"|country=="Iran, Islamic Republic of"
replace ccode = 630 if country=="Iran"|country=="Iran (I.R.)"
replace ccode = 630 if country=="Iran"
replace ccode = 630 if country=="Iran, Islamic Rep."|country=="Islamic Republic of Iran"
replace ccode = 640 if country=="Turkey"
replace ccode = 645 if country=="Iraq"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt, Arab Rep."
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syrian Arab Republic"
replace ccode = 660 if country=="Lebanon"
replace ccode = 663 if country=="Jordan"
replace ccode = 666 if country=="Israel"|country=="Israel and Occupied Territories**"|country=="Israel in pre-1967 borders"
replace ccode = 670 if country=="Saudi Arabia"
replace ccode = 678 if country=="Yemen Arab Republic"|country=="Yemen (A.R.)"|country=="Yemen, N."|country=="Yemen (AR)"|country=="Yemen North"
replace ccode = 679 if country=="Yemen, Rep."|country=="Yemen"|country=="Yemen, Republic of"
replace ccode = 680 if country=="Yemen (PDR)"|country=="Yemen, S."|country=="Yemen, South"|country=="SouthYemen"|country=="Yemen People's Republic"|country=="Yemen  (PDR)"|country=="Yemen South"
replace ccode = 690 if country=="Kuwait"
replace ccode = 692 if country=="Bahrain"
replace ccode = 694 if country=="Qatar"
replace ccode = 696 if country=="Unit. Arab Em."
replace ccode = 696 if country=="United Arab Emirates"|country=="UAE"|country=="United Arab Emirates#"
replace ccode = 698 if country=="Oman"
replace ccode = 70 if country=="Mexico"
replace ccode = 700 if country=="Afghanistan"
replace ccode = 701 if country=="Turkmenistan"
replace ccode = 702 if country=="Tajikistan"
replace ccode = 703 if country=="Kyrgyz Republic"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 704 if country=="Uzbekistan"
replace ccode = 705 if country=="Kazakhstan"
replace ccode = 710 if country=="China (PRC)"
replace ccode = 710 if country=="China"
replace ccode = 710 if country=="China, P. R."
replace ccode = 710 if country=="PRC"
replace ccode = 711 if country=="China: Macao SAR"|country=="Macao, China"
replace ccode = 711 if country=="Macao SAR, China"|country=="China, Macao SAR"
replace ccode = 711 if country=="Macao"|country=="Macau"
replace ccode = 712 if country=="Mongolia"
replace ccode = 713 if country=="Taiwan (ROC)"|country=="Taiwan Province of China"|country=="Taiwan, China"|country=="Taiwan, Province of China"
replace ccode = 713 if country=="Taiwan"
replace ccode = 713 if country=="Taiwan"
replace ccode = 731 if country=="Korea (North)"|country=="Korea North"|country=="Korea, Democratic People's Republic of"
replace ccode = 731 if country=="Korea, Dem. Rep."|country=="Democratic People's Republic of Korea"|country=="D.P.R. Korea"
replace ccode = 731 if country=="North Korea"|country=="Korea, North"
replace ccode = 731 if country=="PRK"|country=="North Korea (Democratc People's Republic of Korea)"
replace ccode = 732 if country=="Korea (South)"|country=="Korea South"
replace ccode = 732 if country=="Korea, Rep."|country=="Korea"|country=="Korea (Rep.)"
replace ccode = 732 if country=="Korea, Republic of"
replace ccode = 732 if country=="Korea, South"
replace ccode = 732 if country=="ROK"
replace ccode = 732 if country=="South Korea"|country=="Korea, South"|country=="Republic of Korea"|country=="South Korea (Republic of Korea)"
replace ccode = 740 if country=="Japan"
replace ccode = 750 if country=="India"
replace ccode = 760 if country=="Bhutan"
replace ccode = 770 if country=="Pakistan"
replace ccode = 771 if country=="Bangladesh"
replace ccode = 775 if country=="Burma (Myanmar)"
replace ccode = 775 if country=="Burma"
replace ccode = 775 if country=="Myanmar"|country=="Myanmar (Burma)"
replace ccode = 780 if country=="Sri Lanka"
replace ccode = 781 if country=="Maldives"
replace ccode = 790 if country=="Nepal"
replace ccode = 80 if country=="Belize"
replace ccode = 800 if country=="Thailand"
replace ccode = 811 if country=="Cambodia"
replace ccode = 812 if country=="Lao PDR"|country=="Lao P.D.R."
replace ccode = 812 if country=="Lao People's Democratic Republic"
replace ccode = 812 if country=="Lao"|country=="Lao People's DR"
replace ccode = 812 if country=="Laos"
replace ccode = 812 if country=="Laos"
replace ccode = 817 if country=="Vietnam, S."|country=="South Vietnam"|country=="Vietnam South"
replace ccode = 818 if country=="Viet Nam"|country=="Vietnam, Socialist Republic of"
replace ccode = 818 if country=="Vietnam"|country=="Vietnam, N."
replace ccode = 820 if country=="Malaysia"
replace ccode = 830 if country=="Singapore"
replace ccode = 835 if country=="Brunei Darussalam"
replace ccode = 835 if country=="Brunei"
replace ccode = 835 if country=="Brunei"
replace ccode = 840 if country=="Philippines"|country=="Philippines +"
replace ccode = 850 if country=="Indonesia"
replace ccode = 860 if country=="East Timor"|country=="East Timor (Timor L'este)"
replace ccode = 860 if country=="Timor-Leste"|country=="Timor Leste"
replace ccode = 90 if country=="Guatemala"
replace ccode = 900 if country=="Australia"
replace ccode = 91 if country=="Honduras"
replace ccode = 910 if country=="P.N. Guinea"
replace ccode = 910 if country=="Papua New Guinea"|country=="P.N. Guinea"|country=="P. N. Guinea"
replace ccode = 92 if country=="El Salvador"
replace ccode = 920 if country=="New Zealand"
replace ccode = 93 if country=="Nicaragua"
replace ccode = 935 if country=="Vanuatu"
replace ccode = 94 if country=="Costa Rica"
replace ccode = 940 if country=="Solomon Is."
replace ccode = 940 if country=="Solomon Islands"
replace ccode = 941 if country=="Northern Mariana Islands"
replace ccode = 946 if country=="Kiribati"
replace ccode = 947 if country=="Tuvalu"
replace ccode = 95 if country=="Panama"
replace ccode = 950 if country=="Fiji"
replace ccode = 951 if country=="French Polynesia"
replace ccode = 952 if country=="New Caledonia"
replace ccode = 955 if country=="Tonga"
replace ccode = 983 if country=="Marshall Islands"
replace ccode = 986 if country=="Palau"
replace ccode = 987 if country=="Micronesia (Federated States of)"|country=="Micronesia (FS of)"|country=="Micronesia, Federated States of"
replace ccode = 987 if country=="Micronesia"
replace ccode = 987 if country=="Micronesia, Fed. Sts."
replace ccode = 990 if country=="Samoa"|country=="W. Samoa"|country=="Western Samoa"
replace ccode = 991 if country=="Guam"
replace ccode = 992 if country=="Nauru"
replace ccode = 993 if country=="Niue"
replace ccode = 994 if country=="Cook Islands"
replace ccode = 995 if country=="American Samoa"

gen y=autendyr-autstartyr+1
expand y, gen(autyrsall)
sort ccode year
bys ccode year: gen sum = sum(autocratization)
replace year = year+sum-1
drop sum y
gen cyear=ccode*10000+year
sort cyear
save "llautocratization.dta", replace


*******************************************
*FREEDOM HOUSE DATA
*******************************************

import excel "Country_and_Territory_Ratings_and_Statuses_FIW_1973-2022.xlsx", sheet("Country Ratings, Statuses ") clear
rename A country
rename B PR1972
rename C CL1972
rename D STAT1972
rename E PR1973
rename F CL1973
rename G STAT1973
rename H PR1974
rename I CL1974
rename J STAT1974
rename K PR1975
rename L CL1975
rename M STAT1975
rename N PR1976
rename O CL1976
rename P STAT1976
rename Q PR1977
rename R CL1977
rename S STAT1977
rename T PR1978
rename U CL1978
rename V STAT1978
rename W PR1979
rename X CL1979
rename Y STAT1979
rename Z PR1980
rename AA CL1980
rename AB STAT1980
rename AC PR1981
rename AD CL1981
rename AE STAT1981
rename AF PR1983
rename AG CL1983
rename AH STAT1983
rename AI PR1984
rename AJ CL1984
rename AK STAT1984
rename AL PR1985
rename AM CL1985
rename AN STAT1985
rename AO PR1986
rename AP CL1986
rename AQ STAT1986
rename AR PR1987
rename AS CL1987
rename AT STAT1987
rename AU PR1988
rename AV CL1988
rename AW STAT1988
rename AX PR1989
rename AY CL1989
rename AZ STAT1989
rename BA PR1990
rename BB CL1990
rename BC STAT1990
rename BD PR1991
rename BE CL1991
rename BF STAT1991
rename BG PR1992
rename BH CL1992
rename BI STAT1992
rename BJ PR1993
rename BK CL1993
rename BL STAT1993
rename BM PR1994
rename BN CL1994
rename BO STAT1994
rename BP PR1995
rename BQ CL1995
rename BR STAT1995
rename BS PR1996
rename BT CL1996
rename BU STAT1996
rename BV PR1997
rename BW CL1997
rename BX STAT1997
rename BY PR1998
rename BZ CL1998
rename CA STAT1998
rename CB PR1999
rename CC CL1999
rename CD STAT1999
rename CE PR2000
rename CF CL2000
rename CG STAT2000
rename CH PR2001
rename CI CL2001
rename CJ STAT2001
rename CK PR2002
rename CL CL2002
rename CM STAT2002
rename CN PR2003
rename CO CL2003
rename CP STAT2003
rename CQ PR2004
rename CR CL2004
rename CS STAT2004
rename CT PR2005
rename CU CL2005
rename CV STAT2005
rename CW PR2006
rename CX CL2006
rename CY STAT2006
rename CZ PR2007
rename DA CL2007
rename DB STAT2007
rename DC PR2008
rename DD CL2008
rename DE STAT2008
rename DF PR2009
rename DG CL2009
rename DH STAT2009
rename DI PR2010
rename DJ CL2010
rename DK STAT2010
rename DL PR2011
rename DM CL2011
rename DN STAT2011
rename DO PR2012
rename DP CL2012
rename DQ STAT2012
rename DR PR2013
rename DS CL2013
rename DT STAT2013
rename DU PR2014
rename DV CL2014
rename DW STAT2014
rename DX PR2015
rename DY CL2015
rename DZ STAT2015
rename EA PR2016
rename EB CL2016
rename EC STAT2016
rename ED PR2017
rename EE CL2017
rename EF STAT2017
rename EG PR2018
rename EH CL2018
rename EI STAT2018
rename EJ PR2019
rename EK CL2019
rename EL STAT2019
rename EM PR2020
rename EN CL2020
rename EO STAT2020
rename EP PR2021
rename EQ CL2021
rename ER STAT2021



drop if _n==1|_n==2|_n==3

replace  PR1972 ="" if  PR1972=="-"
replace  CL1972 ="" if  CL1972=="-"
replace  STAT1972 ="" if  STAT1972=="-"
replace  PR1973 ="" if  PR1973=="-"
replace  CL1973 ="" if  CL1973=="-"
replace  STAT1973 ="" if  STAT1973=="-"
replace  PR1974 ="" if  PR1974=="-"
replace  CL1974 ="" if  CL1974=="-"
replace  STAT1974 ="" if  STAT1974=="-"
replace  PR1975 ="" if  PR1975=="-"
replace  CL1975 ="" if  CL1975=="-"
replace  STAT1975 ="" if  STAT1975=="-"
replace  PR1976 ="" if  PR1976=="-"
replace  CL1976 ="" if  CL1976=="-"
replace  STAT1976 ="" if  STAT1976=="-"
replace  PR1977 ="" if  PR1977=="-"
replace  CL1977 ="" if  CL1977=="-"
replace  STAT1977 ="" if  STAT1977=="-"
replace  PR1978 ="" if  PR1978=="-"
replace  CL1978 ="" if  CL1978=="-"
replace  STAT1978 ="" if  STAT1978=="-"
replace  PR1979 ="" if  PR1979=="-"
replace  CL1979 ="" if  CL1979=="-"
replace  STAT1979 ="" if  STAT1979=="-"
replace  PR1980 ="" if  PR1980=="-"
replace  CL1980 ="" if  CL1980=="-"
replace  STAT1980 ="" if  STAT1980=="-"
replace  PR1981 ="" if  PR1981=="-"
replace  CL1981 ="" if  CL1981=="-"
replace  STAT1981 ="" if  STAT1981=="-"
replace  PR1983 ="" if  PR1983=="-"
replace  CL1983 ="" if  CL1983=="-"
replace  STAT1983 ="" if  STAT1983=="-"
replace  PR1984 ="" if  PR1984=="-"
replace  CL1984 ="" if  CL1984=="-"
replace  STAT1984 ="" if  STAT1984=="-"
replace  PR1985 ="" if  PR1985=="-"
replace  CL1985 ="" if  CL1985=="-"
replace  STAT1985 ="" if  STAT1985=="-"
replace  PR1986 ="" if  PR1986=="-"
replace  CL1986 ="" if  CL1986=="-"
replace  STAT1986 ="" if  STAT1986=="-"
replace  PR1987 ="" if  PR1987=="-"
replace  CL1987 ="" if  CL1987=="-"
replace  STAT1987 ="" if  STAT1987=="-"
replace  PR1988 ="" if  PR1988=="-"
replace  CL1988 ="" if  CL1988=="-"
replace  STAT1988 ="" if  STAT1988=="-"
replace  PR1989 ="" if  PR1989=="-"
replace  CL1989 ="" if  CL1989=="-"
replace  STAT1989 ="" if  STAT1989=="-"
replace  PR1990 ="" if  PR1990=="-"
replace  CL1990 ="" if  CL1990=="-"
replace  STAT1990 ="" if  STAT1990=="-"
replace  PR1991 ="" if  PR1991=="-"
replace  CL1991 ="" if  CL1991=="-"
replace  STAT1991 ="" if  STAT1991=="-"
replace  PR1992 ="" if  PR1992=="-"
replace  CL1992 ="" if  CL1992=="-"
replace  STAT1992 ="" if  STAT1992=="-"
replace  PR1993 ="" if  PR1993=="-"
replace  CL1993 ="" if  CL1993=="-"
replace  STAT1993 ="" if  STAT1993=="-"
replace  PR1994 ="" if  PR1994=="-"
replace  CL1994 ="" if  CL1994=="-"
replace  STAT1994 ="" if  STAT1994=="-"
replace  PR1995 ="" if  PR1995=="-"
replace  CL1995 ="" if  CL1995=="-"
replace  STAT1995 ="" if  STAT1995=="-"
replace  PR1996 ="" if  PR1996=="-"
replace  CL1996 ="" if  CL1996=="-"
replace  STAT1996 ="" if  STAT1996=="-"
replace  PR1997 ="" if  PR1997=="-"
replace  CL1997 ="" if  CL1997=="-"
replace  STAT1997 ="" if  STAT1997=="-"
replace  PR1998 ="" if  PR1998=="-"
replace  CL1998 ="" if  CL1998=="-"
replace  STAT1998 ="" if  STAT1998=="-"
replace  PR1999 ="" if  PR1999=="-"
replace  CL1999 ="" if  CL1999=="-"
replace  STAT1999 ="" if  STAT1999=="-"
replace  PR2000 ="" if  PR2000=="-"
replace  CL2000 ="" if  CL2000=="-"
replace  STAT2000 ="" if  STAT2000=="-"
replace  PR2001 ="" if  PR2001=="-"
replace  CL2001 ="" if  CL2001=="-"
replace  STAT2001 ="" if  STAT2001=="-"
replace  PR2002 ="" if  PR2002=="-"
replace  CL2002 ="" if  CL2002=="-"
replace  STAT2002 ="" if  STAT2002=="-"
replace  PR2003 ="" if  PR2003=="-"
replace  CL2003 ="" if  CL2003=="-"
replace  STAT2003 ="" if  STAT2003=="-"
replace  PR2004 ="" if  PR2004=="-"
replace  CL2004 ="" if  CL2004=="-"
replace  STAT2004 ="" if  STAT2004=="-"
replace  PR2005 ="" if  PR2005=="-"
replace  CL2005 ="" if  CL2005=="-"
replace  STAT2005 ="" if  STAT2005=="-"
replace  PR2006 ="" if  PR2006=="-"
replace  CL2006 ="" if  CL2006=="-"
replace  STAT2006 ="" if  STAT2006=="-"
replace  PR2007 ="" if  PR2007=="-"
replace  CL2007 ="" if  CL2007=="-"
replace  STAT2007 ="" if  STAT2007=="-"
replace  PR2008 ="" if  PR2008=="-"
replace  CL2008 ="" if  CL2008=="-"
replace  STAT2008 ="" if  STAT2008=="-"
replace  PR2009 ="" if  PR2009=="-"
replace  CL2009 ="" if  CL2009=="-"
replace  STAT2009 ="" if  STAT2009=="-"
replace  PR2010 ="" if  PR2010=="-"
replace  CL2010 ="" if  CL2010=="-"
replace  STAT2010 ="" if  STAT2010=="-"
replace  PR2011 ="" if  PR2011=="-"
replace  CL2011 ="" if  CL2011=="-"
replace  STAT2011 ="" if  STAT2011=="-"
replace  PR2012 ="" if  PR2012=="-"
replace  CL2012 ="" if  CL2012=="-"
replace  STAT2012 ="" if  STAT2012=="-"
replace  PR2013 ="" if  PR2013=="-"
replace  CL2013 ="" if  CL2013=="-"
replace  STAT2013 ="" if  STAT2013=="-"
replace  PR2014 ="" if  PR2014=="-"
replace  CL2014 ="" if  CL2014=="-"
replace  STAT2014 ="" if  STAT2014=="-"
replace  PR2015 ="" if  PR2015=="-"
replace  CL2015 ="" if  CL2015=="-"
replace  STAT2015 ="" if  STAT2015=="-"
replace  PR2016 ="" if  PR2016=="-"
replace  CL2016 ="" if  CL2016=="-"
replace  STAT2016 ="" if  STAT2016=="-"
replace  PR2017 ="" if  PR2017=="-"
replace  CL2017 ="" if  CL2017=="-"
replace  STAT2017 ="" if  STAT2017=="-"
replace  PR2018 ="" if  PR2018=="-"
replace  CL2018 ="" if  CL2018=="-"
replace  STAT2018 ="" if  STAT2018=="-"
replace  PR2019 ="" if  PR2019=="-"
replace  CL2019 ="" if  CL2019=="-"
replace  STAT2019 ="" if  STAT2019=="-"
replace  PR2020 ="" if  PR2020=="-"
replace  CL2020 ="" if  CL2020=="-"
replace  STAT2020 ="" if  STAT2020=="-"
replace  PR2021 ="" if  PR2021=="-"
replace  CL2021 ="" if  CL2021=="-"
replace  STAT2021 ="" if  STAT2021=="-"


replace PR1972="5" if country=="South Africa"
replace CL1972="6" if country=="South Africa"

destring  CL1972, replace
destring  CL1973, replace
destring  CL1974, replace
destring  CL1975, replace
destring  CL1976, replace
destring  CL1977, replace
destring  CL1978, replace
destring  CL1979, replace
destring  CL1980, replace
destring  CL1981, replace
destring  CL1983, replace
destring  CL1984, replace
destring  CL1985, replace
destring  CL1986, replace
destring  CL1987, replace
destring  CL1988, replace
destring  CL1989, replace
destring  CL1990, replace
destring  CL1991, replace
destring  CL1992, replace
destring  CL1993, replace
destring  CL1994, replace
destring  CL1995, replace
destring  CL1996, replace
destring  CL1997, replace
destring  CL1998, replace
destring  CL1999, replace
destring  CL2000, replace
destring  CL2001, replace
destring  CL2002, replace
destring  CL2003, replace
destring  CL2004, replace
destring  CL2005, replace
destring  CL2006, replace
destring  CL2007, replace
destring  CL2008, replace
destring  CL2009, replace
destring  CL2010, replace
destring  CL2011, replace
destring  CL2012, replace
destring  CL2013, replace
destring  CL2014, replace
destring  CL2015, replace
destring  CL2016, replace
destring  CL2017, replace
destring  CL2018, replace
destring  CL2019, replace
destring  CL2020, replace
destring  CL2021, replace
destring  PR1972, replace
destring  PR1973, replace
destring  PR1974, replace
destring  PR1975, replace
destring  PR1976, replace
destring  PR1977, replace
destring  PR1978, replace
destring  PR1979, replace
destring  PR1980, replace
destring  PR1981, replace
destring  PR1983, replace
destring  PR1984, replace
destring  PR1985, replace
destring  PR1986, replace
destring  PR1987, replace
destring  PR1988, replace
destring  PR1989, replace
destring  PR1990, replace
destring  PR1991, replace
destring  PR1992, replace
destring  PR1993, replace
destring  PR1994, replace
destring  PR1995, replace
destring  PR1996, replace
destring  PR1997, replace
destring  PR1998, replace
destring  PR1999, replace
destring  PR2000, replace
destring  PR2001, replace
destring  PR2002, replace
destring  PR2003, replace
destring  PR2004, replace
destring  PR2005, replace
destring  PR2006, replace
destring  PR2007, replace
destring  PR2008, replace
destring  PR2009, replace
destring  PR2010, replace
destring  PR2011, replace
destring  PR2012, replace
destring  PR2013, replace
destring  PR2014, replace
destring  PR2015, replace
destring  PR2016, replace
destring  PR2017, replace
destring  PR2018, replace
destring  PR2019, replace
destring  PR2020, replace
destring  PR2021, replace

gen ccode=.
replace ccode = 100 if country=="Colombia"
replace ccode = 1001 if country=="Hong Kong"|country=="China: Hong Kong SAR"|country=="Hong Kong SAR"|country=="Hong Kong SAR, China"|country=="China, Hong Kong SAR"|country=="Hong Kong, China"
replace ccode = 1002 if country=="Puerto Rico"
replace ccode = 1005 if country=="Palestine"|country=="State of Palestine"|country=="Palestinian Territories"|country=="West Bank and Gaza Strip"|country=="West Bank and Gaza"|country=="Israeli-Occupied Territories and Palestinian Authority"|country=="Palestinian Authority"|country=="West Bank"|country=="Palestine, State of"
replace ccode = 101 if country=="Venezuela"|country=="Venezuela (Bolivarian Republic of)"|country=="Venezuela, RB"|country=="Venezuela, Bolivarian Republic of"
replace ccode = 110 if country=="Guyana"
replace ccode = 115 if country=="Suriname"
replace ccode = 130 if country=="Ecuador"
replace ccode = 135 if country=="Peru"
replace ccode = 140 if country=="Brazil"
replace ccode = 145 if country=="Bolivia"|country=="Bolivia (Plurinational State of)"|country=="Bolivia, Plurinational State of"
replace ccode = 150 if country=="Paraguay"
replace ccode = 155 if country=="Chile"
replace ccode = 160 if country=="Argentina"
replace ccode = 165 if country=="Uruguay"
replace ccode = 166 if country=="Faeroe Islands"
replace ccode = 2 if country=="United States of America"|country=="United States"|country=="USA"|country=="US"
replace ccode = 20 if country=="Canada"
replace ccode = 200 if country=="Great Britain"|country=="United Kingdom"|country=="UK"
replace ccode = 201 if country=="Channel Islands"
replace ccode = 202 if country=="Isle of Man"
replace ccode = 205 if country=="Ireland"
replace ccode = 210 if country=="Netherlands"
replace ccode = 211 if country=="Belgium"
replace ccode = 212 if country=="Luxembourg"
replace ccode = 220 if country=="France"
replace ccode = 221 if country=="Monaco"
replace ccode = 223 if country=="Liechtenstein"
replace ccode = 225 if country=="Switzerland"
replace ccode = 230 if country=="Spain"
replace ccode = 232 if country=="Andorra"
replace ccode = 235 if country=="Portugal"
replace ccode = 255 if country=="FRG/Germany"|country=="Germany, W. "|country=="Germany, West"|country=="Germany"|country=="West Germany (FRG)"|country=="German Federal Republic"|country=="Germany West"
replace ccode = 265 if country=="GDR"|country=="Germany, E. "|country=="Germany, East"|country=="East Germany (GDR)"|country=="Germany East"|country=="German Democratic Republic"
replace ccode = 290 if country=="Poland"
replace ccode = 305 if country=="Austria"
replace ccode = 31 if country=="Bahamas"
replace ccode = 31 if country=="Bahamas"|country=="Bahamas, The"|country=="The Bahamas"
replace ccode = 310 if country=="Hungary"
replace ccode = 316 if country=="Czech Rep."|country=="Czech Republic"|country=="Czechia"
replace ccode = 316 if country=="Czechoslovakia"
replace ccode = 317 if country=="Slovak Republic"|country=="Slovakia"|country=="Slovak Rep."
replace ccode = 32 if country=="Aruba"
replace ccode = 325 if country=="Italy"
replace ccode = 33 if country=="Bermuda"
replace ccode = 331 if country=="San Marino"
replace ccode = 338 if country=="Malta"
replace ccode = 339 if country=="Albania"
replace ccode = 341 if country=="Montenegro"
replace ccode = 342 if country=="Serbia and Montenegro"|country=="Serbia-Montenegro"
replace ccode = 342 if country=="Serbia"
replace ccode = 342 if country=="Yugoslavia"
replace ccode = 343 if country=="F.Y.R. Macedonia"|country=="Macedonia, FYR"|country=="FYR Macedonia"|country=="TFYR of Macedonia"|country=="Macedonia"|country=="The former Yugoslav Republic of Macedonia"|country=="Macedonia, the former Yugoslav Republic of"|country=="TFYR Macedonia"|country=="North Macedonia"
replace ccode = 344 if country=="Croatia"
replace ccode = 346 if country=="Bosnia and Herzegovina"|country=="Bosnia"|country=="Bosnia-Herz"|country=="Bosnia-Herzegovina"|country=="Bosnia & Herzegovina"
replace ccode = 347 if country=="Kosovo"
replace ccode = 349 if country=="Slovenia"
replace ccode = 350 if country=="Greece"
replace ccode = 352 if country=="Cyprus"|country=="Cyprus (Greek)"
replace ccode = 355 if country=="Bulgaria"
replace ccode = 359 if country=="Moldova"|country=="Republic of Moldova"|country=="Moldova, Republic of"
replace ccode = 360 if country=="Romania"
replace ccode = 365 if country=="Russia"|country=="Russian Federation"|country=="Russia/USSR"|country=="USSR"|country=="Soviet Union"
replace ccode = 366 if country=="Estonia"
replace ccode = 367 if country=="Latvia"
replace ccode = 368 if country=="Lithuania"
replace ccode = 369 if country=="Ukraine"
replace ccode = 370 if country=="Belarus"
replace ccode = 371 if country=="Armenia"
replace ccode = 372 if country=="Georgia"
replace ccode = 373 if country=="Azerbaijan"
replace ccode = 375 if country=="Finland"
replace ccode = 380 if country=="Sweden"
replace ccode = 385 if country=="Norway"
replace ccode = 390 if country=="Denmark"
replace ccode = 395 if country=="Iceland"
replace ccode = 396 if country=="Greenland"
replace ccode = 40 if country=="Cuba"
replace ccode = 402 if country=="C. Verde Is."|country=="Cabo Verde"|country=="Cape Verde"
replace ccode = 403 if country=="Sao Tome and Principe"|country=="São Tomé and Príncipe"|country=="Sao Tome"|country=="S. Tomé & Principe"
replace ccode = 404 if country=="Guinea-Bissau"|country=="Guinea Bissau"
replace ccode = 41 if country=="Haiti"
replace ccode = 411 if country=="Eq. Guinea"|country=="Equatorial Guinea"
replace ccode = 42 if country=="Dom. Rep."|country=="Dominican Rep."|country=="Dominican Republic"|country=="Dominican Rep"
replace ccode = 420 if country=="Gambia"|country=="Gambia, The"|country=="The Gambia"
replace ccode = 432 if country=="Mali"
replace ccode = 433 if country=="Senegal"
replace ccode = 434 if country=="Benin"
replace ccode = 435 if country=="Mauritania"
replace ccode = 436 if country=="Niger"
replace ccode = 437 if country=="C?te d'Ivoire"|country=="Cote d`Ivoire"|country=="Côte d´Ivoire"|country=="Côte d’Ivoire"|country=="Côte d'Ivoire"|country=="Cote d'Ivoire"|country=="Ivory Coast (Cote d'Ivoire)"|country=="Ivory Coast"|country=="Côte d'Ivoire"|country=="Côte D'Ivoire"|country=="Cote D'Ivoire"
replace ccode = 438 if country=="Guinea"
replace ccode = 439 if country=="Burkina Faso"
replace ccode = 450 if country=="Liberia"
replace ccode = 451 if country=="Sierra Leone"
replace ccode = 452 if country=="Ghana"
replace ccode = 461 if country=="Togo"
replace ccode = 471 if country=="Cameroon"
replace ccode = 475 if country=="Nigeria"
replace ccode = 481 if country=="Gabon"
replace ccode = 482 if country=="Cent. Af. Rep."
replace ccode = 482 if country=="Central Afr. Rep."|country=="Cen African Rep"|country=="Central African Rep."
replace ccode = 482 if country=="Central African Republic"
replace ccode = 483 if country=="Chad"
replace ccode = 484 if country=="Congo (Brazzaville)"
replace ccode = 484 if country=="Congo Brazzaville"|country=="Congo-Brz"
replace ccode = 484 if country=="Congo Republic"
replace ccode = 484 if country=="Congo"
replace ccode = 484 if country=="Congo, Rep. Of"
replace ccode = 484 if country=="Congo, Rep."|country=="Congo-Brazzaville"|country=="Republic of Congo"|country=="Congo, Republic"|country=="Republic of the Congo"
replace ccode = 484 if country=="Congo, Republic of"|country=="Congo, Republic of (Brazzaville)"
replace ccode = 484 if country=="Congo, Republicof"|country=="Congo (Rep.)"
replace ccode = 490 if country=="Congo (Democratic Republic of the)"|country=="Congo, the Democratic Republic of the"|country=="Congo (Dem. Rep.)"|country==" Congo, D.R."|country=="Congo Kinshasa"
replace ccode = 490 if country=="Congo (DRC)"|country=="Congo, Democratic Republic of (Kinshasa)"|country=="Zaire (Democ Republic Congo)"|country=="Congo/Zaire"
replace ccode = 490 if country=="Congo (Kinshasa)"|country=="Congo-Kinshasa"
replace ccode = 490 if country=="Congo, Dem. R."
replace ccode = 490 if country=="Congo, Dem. Rep."|country=="Congo, Democratic Republic"|country=="Democratic Republic of Congo"|country=="Zaire"
replace ccode = 490 if country=="Congo, Democratic Republic of"|country=="Congo, Dem. Rep."|country=="D.R. of the Congo"
replace ccode = 490 if country=="Democratic Republic of the Congo"
replace ccode = 500 if country=="Uganda"
replace ccode = 501 if country=="Kenya"
replace ccode = 51 if country=="Jamaica"
replace ccode = 510 if country=="Tanzania (United Republic of)"|country=="U.R. of Tanzania: Mainland"|country=="Tanzania, United Republic of"
replace ccode = 510 if country=="Tanzania"|country=="United Republic of Tanzania"
replace ccode = 516 if country=="Burundi"
replace ccode = 517 if country=="Rwanda"
replace ccode = 52 if country=="Trinidad & Tob."
replace ccode = 52 if country=="Trinidad & Tobago"
replace ccode = 52 if country=="Trinidad and Tobago"
replace ccode = 52 if country=="Trinidad-Tobago"
replace ccode = 520 if country=="Somalia"
replace ccode = 522 if country=="Djibouti"
replace ccode = 53 if country=="Barbados"
replace ccode = 530 if country=="Ethiopia"
replace ccode = 531 if country=="Eritrea"
replace ccode = 54 if country=="Dominica"
replace ccode = 540 if country=="Angola"
replace ccode = 541 if country=="Mozambique"
replace ccode = 55 if country=="Grenada"
replace ccode = 551 if country=="Zambia"
replace ccode = 552 if country=="Zimbabwe"|country=="Rhodesia"
replace ccode = 553 if country=="Malawi"
replace ccode = 56 if country=="Saint Lucia"
replace ccode = 56 if country=="St. Lucia"
replace ccode = 560 if country=="S. Africa"
replace ccode = 560 if country=="South Africa"
replace ccode = 565 if country=="Namibia"
replace ccode = 57 if country=="Saint Vincent and the Grenadines"|country=="St Vincent"
replace ccode = 57 if country=="St. Vincent & Grenadines"
replace ccode = 57 if country=="St. Vincent and Grenadines"
replace ccode = 57 if country=="St. Vincent and the Grenadines"
replace ccode = 570 if country=="Lesotho"
replace ccode = 571 if country=="Botswana"
replace ccode = 572 if country=="Swaziland"|country=="Eswatini"
replace ccode = 58 if country=="Antigua and Barbuda"|country=="Antigua & Barbuda"
replace ccode = 580 if country=="Madagascar"
replace ccode = 581 if country=="Comoro Is."
replace ccode = 581 if country=="Comoros"
replace ccode = 59 if country=="Cayman Islands"
replace ccode = 590 if country=="Mauritius"
replace ccode = 591 if country=="Seychelles"
replace ccode = 60 if country=="Saint Kitts & Nevis"|country=="Saint Kitts and Nevis"|country=="St Kitts"
replace ccode = 60 if country=="St. Kitts and Nevis"
replace ccode = 60 if country=="St.Kitts & Nevis"
replace ccode = 600 if country=="Morocco"
replace ccode = 61 if country=="Curacao"
replace ccode = 615 if country=="Algeria"
replace ccode = 616 if country=="Tunisia"
replace ccode = 62 if country=="Turks and Caicos Islands"
replace ccode = 620 if country=="Libya"
replace ccode = 625 if country=="Sudan"|country=="Sudan (Former)"
replace ccode = 626 if country=="South Sudan"
replace ccode = 63 if country=="Virgin Islands (U.S.)"
replace ccode = 630 if country=="Iran (Islamic Republic of)"|country=="Iran, Islamic Republic of"
replace ccode = 630 if country=="Iran"|country=="Iran (I.R.)"
replace ccode = 630 if country=="Iran"
replace ccode = 630 if country=="Iran, Islamic Rep."|country=="Islamic Republic of Iran"
replace ccode = 640 if country=="Turkey"
replace ccode = 645 if country=="Iraq"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt, Arab Rep."
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syrian Arab Republic"
replace ccode = 660 if country=="Lebanon"
replace ccode = 663 if country=="Jordan"
replace ccode = 666 if country=="Israel"|country=="Israel and Occupied Territories**"|country=="Israel in pre-1967 borders"
replace ccode = 670 if country=="Saudi Arabia"
replace ccode = 678 if country=="Yemen Arab Republic"|country=="Yemen (A.R.)"|country=="Yemen, N."|country=="Yemen (AR)"|country=="Yemen North"
replace ccode = 679 if country=="Yemen, Rep."|country=="Yemen"|country=="Yemen, Republic of"
replace ccode = 680 if country=="Yemen (PDR)"|country=="Yemen, S."|country=="Yemen, South"|country=="SouthYemen"|country=="Yemen People's Republic"|country=="Yemen  (PDR)"|country=="Yemen South"
replace ccode = 690 if country=="Kuwait"
replace ccode = 692 if country=="Bahrain"
replace ccode = 694 if country=="Qatar"
replace ccode = 696 if country=="Unit. Arab Em."
replace ccode = 696 if country=="United Arab Emirates"|country=="UAE"|country=="United Arab Emirates#"
replace ccode = 698 if country=="Oman"
replace ccode = 70 if country=="Mexico"
replace ccode = 700 if country=="Afghanistan"
replace ccode = 701 if country=="Turkmenistan"
replace ccode = 702 if country=="Tajikistan"
replace ccode = 703 if country=="Kyrgyz Republic"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 704 if country=="Uzbekistan"
replace ccode = 705 if country=="Kazakhstan"
replace ccode = 710 if country=="China (PRC)"
replace ccode = 710 if country=="China"
replace ccode = 710 if country=="China, P. R."
replace ccode = 710 if country=="PRC"
replace ccode = 711 if country=="China: Macao SAR"|country=="Macao, China"
replace ccode = 711 if country=="Macao SAR, China"|country=="China, Macao SAR"
replace ccode = 711 if country=="Macao"|country=="Macau"
replace ccode = 712 if country=="Mongolia"
replace ccode = 713 if country=="Taiwan (ROC)"|country=="Taiwan Province of China"|country=="Taiwan, China"|country=="Taiwan, Province of China"
replace ccode = 713 if country=="Taiwan"
replace ccode = 713 if country=="Taiwan"
replace ccode = 731 if country=="Korea (North)"|country=="Korea North"|country=="Korea, Democratic People's Republic of"
replace ccode = 731 if country=="Korea, Dem. Rep."|country=="Democratic People's Republic of Korea"|country=="D.P.R. Korea"
replace ccode = 731 if country=="North Korea"|country=="Korea, North"
replace ccode = 731 if country=="PRK"|country=="North Korea (Democratc People's Republic of Korea)"
replace ccode = 732 if country=="Korea (South)"|country=="Korea South"
replace ccode = 732 if country=="Korea, Rep."|country=="Korea"|country=="Korea (Rep.)"
replace ccode = 732 if country=="Korea, Republic of"
replace ccode = 732 if country=="Korea, South"
replace ccode = 732 if country=="ROK"
replace ccode = 732 if country=="South Korea"|country=="Korea, South"|country=="Republic of Korea"|country=="South Korea (Republic of Korea)"
replace ccode = 740 if country=="Japan"
replace ccode = 750 if country=="India"
replace ccode = 760 if country=="Bhutan"
replace ccode = 770 if country=="Pakistan"
replace ccode = 771 if country=="Bangladesh"
replace ccode = 775 if country=="Burma (Myanmar)"
replace ccode = 775 if country=="Burma"
replace ccode = 775 if country=="Myanmar"|country=="Myanmar (Burma)"
replace ccode = 780 if country=="Sri Lanka"
replace ccode = 781 if country=="Maldives"
replace ccode = 790 if country=="Nepal"
replace ccode = 80 if country=="Belize"
replace ccode = 800 if country=="Thailand"
replace ccode = 811 if country=="Cambodia"
replace ccode = 812 if country=="Lao PDR"|country=="Lao P.D.R."
replace ccode = 812 if country=="Lao People's Democratic Republic"
replace ccode = 812 if country=="Lao"|country=="Lao People's DR"
replace ccode = 812 if country=="Laos"
replace ccode = 812 if country=="Laos"
replace ccode = 817 if country=="Vietnam, S."|country=="South Vietnam"|country=="Vietnam South"
replace ccode = 818 if country=="Viet Nam"|country=="Vietnam, Socialist Republic of"
replace ccode = 818 if country=="Vietnam"|country=="Vietnam, N."
replace ccode = 820 if country=="Malaysia"
replace ccode = 830 if country=="Singapore"
replace ccode = 835 if country=="Brunei Darussalam"
replace ccode = 835 if country=="Brunei"
replace ccode = 835 if country=="Brunei"
replace ccode = 840 if country=="Philippines"|country=="Philippines +"
replace ccode = 850 if country=="Indonesia"
replace ccode = 860 if country=="East Timor"|country=="East Timor (Timor L'este)"
replace ccode = 860 if country=="Timor-Leste"|country=="Timor Leste"
replace ccode = 90 if country=="Guatemala"
replace ccode = 900 if country=="Australia"
replace ccode = 91 if country=="Honduras"
replace ccode = 910 if country=="P.N. Guinea"
replace ccode = 910 if country=="Papua New Guinea"|country=="P.N. Guinea"|country=="P. N. Guinea"
replace ccode = 92 if country=="El Salvador"
replace ccode = 920 if country=="New Zealand"
replace ccode = 93 if country=="Nicaragua"
replace ccode = 935 if country=="Vanuatu"
replace ccode = 94 if country=="Costa Rica"
replace ccode = 940 if country=="Solomon Is."
replace ccode = 940 if country=="Solomon Islands"
replace ccode = 941 if country=="Northern Mariana Islands"
replace ccode = 946 if country=="Kiribati"
replace ccode = 947 if country=="Tuvalu"
replace ccode = 95 if country=="Panama"
replace ccode = 950 if country=="Fiji"
replace ccode = 951 if country=="French Polynesia"
replace ccode = 952 if country=="New Caledonia"
replace ccode = 955 if country=="Tonga"
replace ccode = 983 if country=="Marshall Islands"
replace ccode = 986 if country=="Palau"
replace ccode = 987 if country=="Micronesia (Federated States of)"|country=="Micronesia (FS of)"|country=="Micronesia, Federated States of"
replace ccode = 987 if country=="Micronesia"
replace ccode = 987 if country=="Micronesia, Fed. Sts."
replace ccode = 990 if country=="Samoa"|country=="W. Samoa"|country=="Western Samoa"
replace ccode = 991 if country=="Guam"
replace ccode = 992 if country=="Nauru"
replace ccode = 993 if country=="Niue"
replace ccode = 994 if country=="Cook Islands"
replace ccode = 995 if country=="American Samoa"
replace ccode=35 if country=="Anguilla"
replace ccode=36 if country=="Curaçao"
replace ccode=37 if country=="Montserrat"
replace ccode=38 if country=="Sint Maarten (Dutch part)"
replace ccode=39 if country=="British Virgin Islands"
replace ccode=521 if country=="Somaliland"|country=="Somaliland Region"
replace ccode=353 if country=="Northern Cyprus"
drop if ccode==.

*there are errors in Freedom House's data file for 2021. I have corrected using FH's other file at: https://freedomhouse.org/sites/default/files/2022-02/Aggregate_Category_and_Subcategory_Scores_FIW_2003-2022.xlsx

save "fh20.dta", replace

drop STAT* CL*
*combine some duplicates
collapse (mean) PR*, by(ccode)
reshape long PR, i(ccode) j(year)
rename PR fhpol
xtset ccode year
gen fhpolnr = 8-fhpol
gen lfhpolnr = l.fhpolnr
gen cyear=ccode*10000+year
sort cyear
save "fh20pollong.dta", replace
keep ccode year cyear fhpol fhpolnr lfhpolnr
sort cyear
save "fh20polshort.dta", replace
use "fh20.dta", clear
drop STAT* PR*
*combine some duplicates
collapse (mean) CL*, by(ccode)
reshape long CL, i(ccode) j(year)
rename CL fhciv
xtset ccode year
gen cyear=ccode*10000+year
sort cyear
save "fh20civlong.dta", replace
use "fh20.dta", clear
drop CL* PR*
*combine some duplicates
collapse (firstnm) STAT*, by(ccode)
reshape long STAT, i(ccode) j(year)
rename STAT fhstat
xtset ccode year
gen cyear=ccode*10000+year
sort cyear
save "fh20statlong.dta", replace

*For the 100 point FH political scores
import excel "Aggregate_Category_and_Subcategory_Scores_FIW_2003-2022 (1).xlsx", sheet("FIW06-22") firstrow clear
rename CountryTerritory country
gen year = Edition-1
keep country year PR CL
gen ccode=.
replace ccode = 100 if country=="Colombia"
replace ccode = 1001 if country=="Hong Kong"|country=="China: Hong Kong SAR"|country=="Hong Kong SAR"|country=="Hong Kong SAR, China"|country=="China, Hong Kong SAR"|country=="Hong Kong, China"
replace ccode = 1002 if country=="Puerto Rico"
replace ccode = 1005 if country=="Palestine"|country=="State of Palestine"|country=="Palestinian Territories"|country=="West Bank and Gaza Strip"|country=="West Bank and Gaza"|country=="Israeli-Occupied Territories and Palestinian Authority"|country=="Palestinian Authority"|country=="West Bank"|country=="Palestine, State of"
replace ccode = 101 if country=="Venezuela"|country=="Venezuela (Bolivarian Republic of)"|country=="Venezuela, RB"|country=="Venezuela, Bolivarian Republic of"
replace ccode = 110 if country=="Guyana"
replace ccode = 115 if country=="Suriname"
replace ccode = 130 if country=="Ecuador"
replace ccode = 135 if country=="Peru"
replace ccode = 140 if country=="Brazil"
replace ccode = 145 if country=="Bolivia"|country=="Bolivia (Plurinational State of)"|country=="Bolivia, Plurinational State of"
replace ccode = 150 if country=="Paraguay"
replace ccode = 155 if country=="Chile"
replace ccode = 160 if country=="Argentina"
replace ccode = 165 if country=="Uruguay"
replace ccode = 166 if country=="Faeroe Islands"
replace ccode = 2 if country=="United States of America"|country=="United States"|country=="USA"|country=="US"
replace ccode = 20 if country=="Canada"
replace ccode = 200 if country=="Great Britain"|country=="United Kingdom"|country=="UK"
replace ccode = 201 if country=="Channel Islands"
replace ccode = 202 if country=="Isle of Man"
replace ccode = 205 if country=="Ireland"
replace ccode = 210 if country=="Netherlands"
replace ccode = 211 if country=="Belgium"
replace ccode = 212 if country=="Luxembourg"
replace ccode = 220 if country=="France"
replace ccode = 221 if country=="Monaco"
replace ccode = 223 if country=="Liechtenstein"
replace ccode = 225 if country=="Switzerland"
replace ccode = 230 if country=="Spain"
replace ccode = 232 if country=="Andorra"
replace ccode = 235 if country=="Portugal"
replace ccode = 255 if country=="FRG/Germany"|country=="Germany, W. "|country=="Germany, West"|country=="Germany"|country=="West Germany (FRG)"|country=="German Federal Republic"|country=="Germany West"
replace ccode = 265 if country=="GDR"|country=="Germany, E. "|country=="Germany, East"|country=="East Germany (GDR)"|country=="Germany East"|country=="German Democratic Republic"
replace ccode = 290 if country=="Poland"
replace ccode = 305 if country=="Austria"
replace ccode = 31 if country=="Bahamas"
replace ccode = 31 if country=="Bahamas"|country=="Bahamas, The"|country=="The Bahamas"
replace ccode = 310 if country=="Hungary"
replace ccode = 316 if country=="Czech Rep."|country=="Czech Republic"|country=="Czechia"
replace ccode = 316 if country=="Czechoslovakia"
replace ccode = 317 if country=="Slovak Republic"|country=="Slovakia"|country=="Slovak Rep."
replace ccode = 32 if country=="Aruba"
replace ccode = 325 if country=="Italy"
replace ccode = 33 if country=="Bermuda"
replace ccode = 331 if country=="San Marino"
replace ccode = 338 if country=="Malta"
replace ccode = 339 if country=="Albania"
replace ccode = 341 if country=="Montenegro"
replace ccode = 342 if country=="Serbia and Montenegro"|country=="Serbia-Montenegro"
replace ccode = 342 if country=="Serbia"
replace ccode = 342 if country=="Yugoslavia"
replace ccode = 343 if country=="F.Y.R. Macedonia"|country=="Macedonia, FYR"|country=="FYR Macedonia"|country=="TFYR of Macedonia"|country=="Macedonia"|country=="The former Yugoslav Republic of Macedonia"|country=="Macedonia, the former Yugoslav Republic of"|country=="TFYR Macedonia"|country=="North Macedonia"
replace ccode = 344 if country=="Croatia"
replace ccode = 346 if country=="Bosnia and Herzegovina"|country=="Bosnia"|country=="Bosnia-Herz"|country=="Bosnia-Herzegovina"|country=="Bosnia & Herzegovina"
replace ccode = 347 if country=="Kosovo"
replace ccode = 349 if country=="Slovenia"
replace ccode = 350 if country=="Greece"
replace ccode = 352 if country=="Cyprus"|country=="Cyprus (Greek)"
replace ccode = 355 if country=="Bulgaria"
replace ccode = 359 if country=="Moldova"|country=="Republic of Moldova"|country=="Moldova, Republic of"
replace ccode = 360 if country=="Romania"
replace ccode = 365 if country=="Russia"|country=="Russian Federation"|country=="Russia/USSR"|country=="USSR"|country=="Soviet Union"
replace ccode = 366 if country=="Estonia"
replace ccode = 367 if country=="Latvia"
replace ccode = 368 if country=="Lithuania"
replace ccode = 369 if country=="Ukraine"
replace ccode = 370 if country=="Belarus"
replace ccode = 371 if country=="Armenia"
replace ccode = 372 if country=="Georgia"
replace ccode = 373 if country=="Azerbaijan"
replace ccode = 375 if country=="Finland"
replace ccode = 380 if country=="Sweden"
replace ccode = 385 if country=="Norway"
replace ccode = 390 if country=="Denmark"
replace ccode = 395 if country=="Iceland"
replace ccode = 396 if country=="Greenland"
replace ccode = 40 if country=="Cuba"
replace ccode = 402 if country=="C. Verde Is."|country=="Cabo Verde"|country=="Cape Verde"
replace ccode = 403 if country=="Sao Tome and Principe"|country=="São Tomé and Príncipe"|country=="Sao Tome"|country=="S. Tomé & Principe"
replace ccode = 404 if country=="Guinea-Bissau"|country=="Guinea Bissau"
replace ccode = 41 if country=="Haiti"
replace ccode = 411 if country=="Eq. Guinea"|country=="Equatorial Guinea"
replace ccode = 42 if country=="Dom. Rep."|country=="Dominican Rep."|country=="Dominican Republic"|country=="Dominican Rep"
replace ccode = 420 if country=="Gambia"|country=="Gambia, The"|country=="The Gambia"
replace ccode = 432 if country=="Mali"
replace ccode = 433 if country=="Senegal"
replace ccode = 434 if country=="Benin"
replace ccode = 435 if country=="Mauritania"
replace ccode = 436 if country=="Niger"
replace ccode = 437 if country=="C?te d'Ivoire"|country=="Cote d`Ivoire"|country=="Côte d´Ivoire"|country=="Côte d’Ivoire"|country=="Côte d'Ivoire"|country=="Cote d'Ivoire"|country=="Ivory Coast (Cote d'Ivoire)"|country=="Ivory Coast"|country=="Côte d'Ivoire"|country=="Côte D'Ivoire"|country=="Cote D'Ivoire"
replace ccode = 438 if country=="Guinea"
replace ccode = 439 if country=="Burkina Faso"
replace ccode = 450 if country=="Liberia"
replace ccode = 451 if country=="Sierra Leone"
replace ccode = 452 if country=="Ghana"
replace ccode = 461 if country=="Togo"
replace ccode = 471 if country=="Cameroon"
replace ccode = 475 if country=="Nigeria"
replace ccode = 481 if country=="Gabon"
replace ccode = 482 if country=="Cent. Af. Rep."
replace ccode = 482 if country=="Central Afr. Rep."|country=="Cen African Rep"|country=="Central African Rep."
replace ccode = 482 if country=="Central African Republic"
replace ccode = 483 if country=="Chad"
replace ccode = 484 if country=="Congo (Brazzaville)"
replace ccode = 484 if country=="Congo Brazzaville"|country=="Congo-Brz"
replace ccode = 484 if country=="Congo Republic"
replace ccode = 484 if country=="Congo"
replace ccode = 484 if country=="Congo, Rep. Of"
replace ccode = 484 if country=="Congo, Rep."|country=="Congo-Brazzaville"|country=="Republic of Congo"|country=="Congo, Republic"|country=="Republic of the Congo"
replace ccode = 484 if country=="Congo, Republic of"|country=="Congo, Republic of (Brazzaville)"
replace ccode = 484 if country=="Congo, Republicof"|country=="Congo (Rep.)"
replace ccode = 490 if country=="Congo (Democratic Republic of the)"|country=="Congo, the Democratic Republic of the"|country=="Congo (Dem. Rep.)"|country==" Congo, D.R."|country=="Congo Kinshasa"
replace ccode = 490 if country=="Congo (DRC)"|country=="Congo, Democratic Republic of (Kinshasa)"|country=="Zaire (Democ Republic Congo)"|country=="Congo/Zaire"
replace ccode = 490 if country=="Congo (Kinshasa)"|country=="Congo-Kinshasa"
replace ccode = 490 if country=="Congo, Dem. R."
replace ccode = 490 if country=="Congo, Dem. Rep."|country=="Congo, Democratic Republic"|country=="Democratic Republic of Congo"|country=="Zaire"
replace ccode = 490 if country=="Congo, Democratic Republic of"|country=="Congo, Dem. Rep."|country=="D.R. of the Congo"
replace ccode = 490 if country=="Democratic Republic of the Congo"
replace ccode = 500 if country=="Uganda"
replace ccode = 501 if country=="Kenya"
replace ccode = 51 if country=="Jamaica"
replace ccode = 510 if country=="Tanzania (United Republic of)"|country=="U.R. of Tanzania: Mainland"|country=="Tanzania, United Republic of"
replace ccode = 510 if country=="Tanzania"|country=="United Republic of Tanzania"
replace ccode = 516 if country=="Burundi"
replace ccode = 517 if country=="Rwanda"
replace ccode = 52 if country=="Trinidad & Tob."
replace ccode = 52 if country=="Trinidad & Tobago"
replace ccode = 52 if country=="Trinidad and Tobago"
replace ccode = 52 if country=="Trinidad-Tobago"
replace ccode = 520 if country=="Somalia"
replace ccode = 522 if country=="Djibouti"
replace ccode = 53 if country=="Barbados"
replace ccode = 530 if country=="Ethiopia"
replace ccode = 531 if country=="Eritrea"
replace ccode = 54 if country=="Dominica"
replace ccode = 540 if country=="Angola"
replace ccode = 541 if country=="Mozambique"
replace ccode = 55 if country=="Grenada"
replace ccode = 551 if country=="Zambia"
replace ccode = 552 if country=="Zimbabwe"|country=="Rhodesia"
replace ccode = 553 if country=="Malawi"
replace ccode = 56 if country=="Saint Lucia"
replace ccode = 56 if country=="St. Lucia"
replace ccode = 560 if country=="S. Africa"
replace ccode = 560 if country=="South Africa"
replace ccode = 565 if country=="Namibia"
replace ccode = 57 if country=="Saint Vincent and the Grenadines"|country=="St Vincent"
replace ccode = 57 if country=="St. Vincent & Grenadines"
replace ccode = 57 if country=="St. Vincent and Grenadines"
replace ccode = 57 if country=="St. Vincent and the Grenadines"
replace ccode = 570 if country=="Lesotho"
replace ccode = 571 if country=="Botswana"
replace ccode = 572 if country=="Swaziland"|country=="Eswatini"
replace ccode = 58 if country=="Antigua and Barbuda"|country=="Antigua & Barbuda"
replace ccode = 580 if country=="Madagascar"
replace ccode = 581 if country=="Comoro Is."
replace ccode = 581 if country=="Comoros"
replace ccode = 59 if country=="Cayman Islands"
replace ccode = 590 if country=="Mauritius"
replace ccode = 591 if country=="Seychelles"
replace ccode = 60 if country=="Saint Kitts & Nevis"|country=="Saint Kitts and Nevis"|country=="St Kitts"
replace ccode = 60 if country=="St. Kitts and Nevis"
replace ccode = 60 if country=="St.Kitts & Nevis"
replace ccode = 600 if country=="Morocco"
replace ccode = 61 if country=="Curacao"
replace ccode = 615 if country=="Algeria"
replace ccode = 616 if country=="Tunisia"
replace ccode = 62 if country=="Turks and Caicos Islands"
replace ccode = 620 if country=="Libya"
replace ccode = 625 if country=="Sudan"|country=="Sudan (Former)"
replace ccode = 626 if country=="South Sudan"
replace ccode = 63 if country=="Virgin Islands (U.S.)"
replace ccode = 630 if country=="Iran (Islamic Republic of)"|country=="Iran, Islamic Republic of"
replace ccode = 630 if country=="Iran"|country=="Iran (I.R.)"
replace ccode = 630 if country=="Iran"
replace ccode = 630 if country=="Iran, Islamic Rep."|country=="Islamic Republic of Iran"
replace ccode = 640 if country=="Turkey"
replace ccode = 645 if country=="Iraq"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt, Arab Rep."
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syrian Arab Republic"
replace ccode = 660 if country=="Lebanon"
replace ccode = 663 if country=="Jordan"
replace ccode = 666 if country=="Israel"|country=="Israel and Occupied Territories**"|country=="Israel in pre-1967 borders"
replace ccode = 670 if country=="Saudi Arabia"
replace ccode = 678 if country=="Yemen Arab Republic"|country=="Yemen (A.R.)"|country=="Yemen, N."|country=="Yemen (AR)"|country=="Yemen North"
replace ccode = 679 if country=="Yemen, Rep."|country=="Yemen"|country=="Yemen, Republic of"
replace ccode = 680 if country=="Yemen (PDR)"|country=="Yemen, S."|country=="Yemen, South"|country=="SouthYemen"|country=="Yemen People's Republic"|country=="Yemen  (PDR)"|country=="Yemen South"
replace ccode = 690 if country=="Kuwait"
replace ccode = 692 if country=="Bahrain"
replace ccode = 694 if country=="Qatar"
replace ccode = 696 if country=="Unit. Arab Em."
replace ccode = 696 if country=="United Arab Emirates"|country=="UAE"|country=="United Arab Emirates#"
replace ccode = 698 if country=="Oman"
replace ccode = 70 if country=="Mexico"
replace ccode = 700 if country=="Afghanistan"
replace ccode = 701 if country=="Turkmenistan"
replace ccode = 702 if country=="Tajikistan"
replace ccode = 703 if country=="Kyrgyz Republic"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 704 if country=="Uzbekistan"
replace ccode = 705 if country=="Kazakhstan"
replace ccode = 710 if country=="China (PRC)"
replace ccode = 710 if country=="China"
replace ccode = 710 if country=="China, P. R."
replace ccode = 710 if country=="PRC"
replace ccode = 711 if country=="China: Macao SAR"|country=="Macao, China"
replace ccode = 711 if country=="Macao SAR, China"|country=="China, Macao SAR"
replace ccode = 711 if country=="Macao"|country=="Macau"
replace ccode = 712 if country=="Mongolia"
replace ccode = 713 if country=="Taiwan (ROC)"|country=="Taiwan Province of China"|country=="Taiwan, China"|country=="Taiwan, Province of China"
replace ccode = 713 if country=="Taiwan"
replace ccode = 713 if country=="Taiwan"
replace ccode = 731 if country=="Korea (North)"|country=="Korea North"|country=="Korea, Democratic People's Republic of"
replace ccode = 731 if country=="Korea, Dem. Rep."|country=="Democratic People's Republic of Korea"|country=="D.P.R. Korea"
replace ccode = 731 if country=="North Korea"|country=="Korea, North"
replace ccode = 731 if country=="PRK"|country=="North Korea (Democratc People's Republic of Korea)"
replace ccode = 732 if country=="Korea (South)"|country=="Korea South"
replace ccode = 732 if country=="Korea, Rep."|country=="Korea"|country=="Korea (Rep.)"
replace ccode = 732 if country=="Korea, Republic of"
replace ccode = 732 if country=="Korea, South"
replace ccode = 732 if country=="ROK"
replace ccode = 732 if country=="South Korea"|country=="Korea, South"|country=="Republic of Korea"|country=="South Korea (Republic of Korea)"
replace ccode = 740 if country=="Japan"
replace ccode = 750 if country=="India"
replace ccode = 760 if country=="Bhutan"
replace ccode = 770 if country=="Pakistan"
replace ccode = 771 if country=="Bangladesh"
replace ccode = 775 if country=="Burma (Myanmar)"
replace ccode = 775 if country=="Burma"
replace ccode = 775 if country=="Myanmar"|country=="Myanmar (Burma)"
replace ccode = 780 if country=="Sri Lanka"
replace ccode = 781 if country=="Maldives"
replace ccode = 790 if country=="Nepal"
replace ccode = 80 if country=="Belize"
replace ccode = 800 if country=="Thailand"
replace ccode = 811 if country=="Cambodia"
replace ccode = 812 if country=="Lao PDR"|country=="Lao P.D.R."
replace ccode = 812 if country=="Lao People's Democratic Republic"
replace ccode = 812 if country=="Lao"|country=="Lao People's DR"
replace ccode = 812 if country=="Laos"
replace ccode = 812 if country=="Laos"
replace ccode = 817 if country=="Vietnam, S."|country=="South Vietnam"|country=="Vietnam South"
replace ccode = 818 if country=="Viet Nam"|country=="Vietnam, Socialist Republic of"
replace ccode = 818 if country=="Vietnam"|country=="Vietnam, N."
replace ccode = 820 if country=="Malaysia"
replace ccode = 830 if country=="Singapore"
replace ccode = 835 if country=="Brunei Darussalam"
replace ccode = 835 if country=="Brunei"
replace ccode = 835 if country=="Brunei"
replace ccode = 840 if country=="Philippines"|country=="Philippines +"
replace ccode = 850 if country=="Indonesia"
replace ccode = 860 if country=="East Timor"|country=="East Timor (Timor L'este)"
replace ccode = 860 if country=="Timor-Leste"|country=="Timor Leste"
replace ccode = 90 if country=="Guatemala"
replace ccode = 900 if country=="Australia"
replace ccode = 91 if country=="Honduras"
replace ccode = 910 if country=="P.N. Guinea"
replace ccode = 910 if country=="Papua New Guinea"|country=="P.N. Guinea"|country=="P. N. Guinea"
replace ccode = 92 if country=="El Salvador"
replace ccode = 920 if country=="New Zealand"
replace ccode = 93 if country=="Nicaragua"
replace ccode = 935 if country=="Vanuatu"
replace ccode = 94 if country=="Costa Rica"
replace ccode = 940 if country=="Solomon Is."
replace ccode = 940 if country=="Solomon Islands"
replace ccode = 941 if country=="Northern Mariana Islands"
replace ccode = 946 if country=="Kiribati"
replace ccode = 947 if country=="Tuvalu"
replace ccode = 95 if country=="Panama"
replace ccode = 950 if country=="Fiji"
replace ccode = 951 if country=="French Polynesia"
replace ccode = 952 if country=="New Caledonia"
replace ccode = 955 if country=="Tonga"
replace ccode = 983 if country=="Marshall Islands"
replace ccode = 986 if country=="Palau"
replace ccode = 987 if country=="Micronesia (Federated States of)"|country=="Micronesia (FS of)"|country=="Micronesia, Federated States of"
replace ccode = 987 if country=="Micronesia"
replace ccode = 987 if country=="Micronesia, Fed. Sts."
replace ccode = 990 if country=="Samoa"|country=="W. Samoa"|country=="Western Samoa"
replace ccode = 991 if country=="Guam"
replace ccode = 992 if country=="Nauru"
replace ccode = 993 if country=="Niue"
replace ccode = 994 if country=="Cook Islands"
replace ccode = 995 if country=="American Samoa"
replace ccode=35 if country=="Anguilla"
replace ccode=36 if country=="Curaçao"
replace ccode=37 if country=="Montserrat"
replace ccode=38 if country=="Sint Maarten (Dutch part)"
replace ccode=39 if country=="British Virgin Islands"
replace ccode=521 if country=="Somaliland"|country=="Somaliland Region"
replace ccode=353 if country=="Northern Cyprus"
drop if ccode==.
xtset ccode year
gen lPR = l.PR
gen cyear=ccode*10000+year
sort cyear
save "FHPRCL.dta", replace


*********************************************************************************************
*MADDISON 2020 DATA (https://www.rug.nl/ggdc/historicaldevelopment/maddison/data/mpd2020.dta)
*********************************************************************************************

use "mad2020.dta", clear
replace gdppc=. if gdppc==0
drop if countrycode=="SUN"&year>1949
*doubling up of FSU and Russia
drop if country=="Serbia"
*use Former Yugo instead
sort country year
*bys country: ipolate gdppc year, gen(gdppci)
save "mad2020wk.dta", replace
gen ccode = .
replace ccode = 700 if country=="Afghanistan"
replace ccode = 339 if country=="Albania"
replace ccode = 615 if country=="Algeria"
replace ccode = 540 if country=="Angola"
replace ccode = 160 if country=="Argentina"
replace ccode = 371 if country=="Armenia"
replace ccode = 900 if country=="Australia"
replace ccode = 305 if country=="Austria"
replace ccode = 373 if country=="Azerbaijan"
replace ccode = 692 if country=="Bahrain"
replace ccode = 771 if country=="Bangladesh"
replace ccode = 370 if country=="Belarus"
replace ccode = 211 if country=="Belgium"
replace ccode = 434 if country=="Benin"
replace ccode = 145 if country=="Bolivia"
replace ccode = 346 if country=="Bosnia and Herzegovina"
replace ccode = 571 if country=="Botswana"
replace ccode = 140 if country=="Brazil"
replace ccode = 355 if country=="Bulgaria"
replace ccode = 439 if country=="Burkina Faso"
replace ccode = 775 if country=="Myanmar"
replace ccode = 516 if country=="Burundi"
replace ccode = 811 if country=="Cambodia"
replace ccode = 471 if country=="Cameroon"
replace ccode = 20 if country=="Canada"
replace ccode = 402 if country=="Cabo Verde"
replace ccode = 482 if country=="Central African Republic"
replace ccode = 483 if country=="Chad"
replace ccode = 155 if country=="Chile"
replace ccode = 710 if country=="China"
replace ccode = 100 if country=="Colombia"
replace ccode = 581 if country=="Comoros"
replace ccode = 484 if country=="Congo"
replace ccode = 94 if country=="Costa Rica"
replace ccode = 437 if country=="C𴥠d'Ivoire"
replace ccode = 437 if country=="Côte d'Ivoire"
replace ccode = 344 if country=="Croatia"
replace ccode = 40 if country=="Cuba"
replace ccode = 316 if country=="Czech Republic"
replace ccode = 315 if country=="Czechoslovakia"
replace ccode = 390 if country=="Denmark"
replace ccode = 522 if country=="Djibouti"
replace ccode = 42 if country=="Dominican Republic"
replace ccode = 130 if country=="Ecuador"
replace ccode = 651 if country=="Egypt"
replace ccode = 92 if country=="El Salvador"
replace ccode = 411 if country=="Equatorial Guinea"
replace ccode = 531 if country=="Eritrea"
replace ccode = 530 if country=="Ethiopia"
replace ccode = 366 if country=="Estonia"
replace ccode = 375 if country=="Finland"
replace ccode = 220 if country=="France"
replace ccode = 481 if country=="Gabon"
replace ccode = 420 if country=="Gambia"
replace ccode = 372 if country=="Georgia"
replace ccode = 255 if country=="Germany"
replace ccode = 452 if country=="Ghana"
replace ccode = 350 if country=="Greece"
replace ccode = 90 if country=="Guatemala"
replace ccode = 438 if country=="Guinea"
replace ccode = 404 if country=="Guinea-Bissau"
replace ccode = 110 if country=="Guyana"
replace ccode = 41 if country=="Ha൩"
replace ccode = 41 if country=="Haiti"
replace ccode = 91 if country=="Honduras"
replace ccode = 1001 if country=="China, Hong Kong SAR"
replace ccode = 310 if country=="Hungary"
replace ccode = 750 if country=="India"
replace ccode = 850 if country=="Indonesia"
replace ccode = 630 if country=="Iran (Islamic Republic of)"
replace ccode = 645 if country=="Iraq"
replace ccode = 205 if country=="Ireland"
replace ccode = 666 if country=="Israel"
replace ccode = 325 if country=="Italy"
replace ccode = 51 if country=="Jamaica"
replace ccode = 740 if country=="Japan"
replace ccode = 663 if country=="Jordan"
replace ccode = 705 if country=="Kazakhstan"
replace ccode = 501 if country=="Kenya"
replace ccode = 347 if country=="Kosovo"
replace ccode = 690 if country=="Kuwait"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 812 if country=="Lao People's DR"
replace ccode = 367 if country=="Latvia"
replace ccode = 660 if country=="Lebanon"
replace ccode = 570 if country=="Lesotho"
replace ccode = 450 if country=="Liberia"
replace ccode = 620 if country=="Libya"
replace ccode = 368 if country=="Lithuania"
replace ccode = 212 if country=="Luxembourg"
replace ccode = 343 if country=="TFYR of Macedonia"
replace ccode = 580 if country=="Madagascar"
replace ccode = 553 if country=="Malawi"
replace ccode = 820 if country=="Malaysia"
replace ccode = 338 if country=="Malta"
replace ccode = 432 if country=="Mali"
replace ccode = 435 if country=="Mauritania"
replace ccode = 590 if country=="Mauritius"
replace ccode = 70 if country=="Mexico"
replace ccode = 359 if country=="Republic of Moldova"
replace ccode = 712 if country=="Mongolia"
replace ccode = 341 if country=="Montenegro"
replace ccode = 600 if country=="Morocco"
replace ccode = 541 if country=="Mozambique"
replace ccode = 565 if country=="Namibia"
replace ccode = 790 if country=="Nepal"
replace ccode = 210 if country=="Netherlands"
replace ccode = 920 if country=="New Zealand"
replace ccode = 93 if country=="Nicaragua"
replace ccode = 436 if country=="Niger"
replace ccode = 475 if country=="Nigeria"
replace ccode = 731 if country=="D.P.R. of Korea"
replace ccode = 385 if country=="Norway"
replace ccode = 698 if country=="Oman"
replace ccode = 770 if country=="Pakistan"
replace ccode = 95 if country=="Panama"
replace ccode = 150 if country=="Paraguay"
replace ccode = 135 if country=="Peru"
replace ccode = 840 if country=="Philippines"
replace ccode = 290 if country=="Poland"
replace ccode = 235 if country=="Portugal"
replace ccode = 1002 if country=="Puerto Rico"
replace ccode = 694 if country=="Qatar"
replace ccode = 360 if country=="Romania"
replace ccode = 365 if country=="Russian Federation"&year>1949|country=="Former USSR"
replace ccode = 517 if country=="Rwanda"
replace ccode = 403 if country=="S䯠Tom顡nd Principe"
replace ccode = 403 if country=="Sao Tome and Principe"
replace ccode = 670 if country=="Saudi Arabia"
replace ccode = 433 if country=="Senegal"
replace ccode = 342 if country=="Serbia"
replace ccode = 348 if country=="Serbia and Montenegro"
replace ccode=345 if country=="Serbia and Montenegro"&year>2002&year<2007
replace ccode = 591 if country=="Seychelles"
replace ccode = 451 if country=="Sierra Leone"
replace ccode = 830 if country=="Singapore"
replace ccode = 317 if country=="Slovakia"
replace ccode = 349 if country=="Slovenia"
replace ccode = 520 if country=="Somalia"
replace ccode = 560 if country=="South Africa"
replace ccode = 732 if country=="Republic of Korea"
replace ccode = 730 if country=="Korea"
replace ccode = 230 if country=="Spain"
replace ccode = 780 if country=="Sri Lanka"
replace ccode = 625 if country=="Sudan (Former)"
replace ccode = 572 if country=="Swaziland"
replace ccode = 380 if country=="Sweden"
replace ccode = 225 if country=="Switzerland"
replace ccode = 652 if country=="Syrian Arab Republic"
replace ccode = 713 if country=="Taiwan, Province of China"
replace ccode = 702 if country=="Tajikistan"
replace ccode = 510 if country=="U.R. of Tanzania: Mainland"
replace ccode = 800 if country=="Thailand"
replace ccode = 461 if country=="Togo"
replace ccode = 365 if country=="Total Former USSR"&year<1992
replace ccode = 52 if country=="Trinidad and Tobago"
replace ccode = 616 if country=="Tunisia"
replace ccode = 640 if country=="Turkey"
replace ccode = 701 if country=="Turkmenistan"
replace ccode = 500 if country=="Uganda"
replace ccode = 369 if country=="Ukraine"
replace ccode = 696 if country=="United Arab Emirates"
replace ccode = 200 if country=="United Kingdom"
replace ccode = 2 if country=="United States"
replace ccode = 165 if country=="Uruguay"
replace ccode = 704 if country=="Uzbekistan"
replace ccode = 101 if country=="Venezuela (Bolivarian Republic of)"
replace ccode = 818 if country=="Viet Nam"
replace ccode = 816 if country=="Vietnam North"
replace ccode = 817 if country=="Vietnam South"
replace ccode = 1005 if country=="West Bank and Gaza"|country=="State of Palestine"
replace ccode = 679 if country=="Yemen"
replace ccode = 678 if country=="Yemen North"
replace ccode = 680 if country=="Yemen South"
replace ccode = 342 if country=="Former Yugoslavia"
replace ccode = 490 if country=="D.R. of the Congo"
replace ccode = 551 if country=="Zambia"
replace ccode = 552 if country=="Zimbabwe"
replace ccode = 56 if country=="Saint Lucia"
replace ccode = 395 if country=="Iceland"
replace ccode = 54 if country=="Dominica"
replace ccode = 352 if country=="Cyprus"
replace ccode = 145 if country=="Bolivia (Plurinational State of)"
replace ccode = 53 if country=="Barbados"
drop if ccode==.
gen cyear=ccode*10000+year
xtset ccode year 
gen madgrowth=((gdppci/l.gdppci)-1)*100
label var madgrowth "growth rate of gdppc; Maddison 2020"
gen lngdppci = ln(gdppci)
label var lngdppci "ln of gdppci; Maddison 2020"
keep country year gdppc gdppci lngdppci pop ccode cyear madgrowth
drop if year<1800
sort cyear
save "mad2020wk.dta", replace


**********************************************************************************************************************
*LEXICAL INDEX OF ELECTORAL DEMOCRACY (https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/29106)
**********************************************************************************************************************

import excel "lied_v6.0.xls", sheet("Ark1") firstrow clear
*create id code
rename cow ccode
*Clean up
*Tibet
drop if ccode==711
replace ccode=255 if ccode==260
*adjust to my country codings* 
drop if ccode==260&year==1945
*not to have both Germany and FRG in 1945*
drop if ccode==260&year==1990
*not to have both Germany and FRG in 1990*
drop if ccode==347&year==1991
*combine Yugoslavia pre 2003 with Serbia and Montenegro 2003-2006
replace ccode = 342 if country=="Yugoslavia"
replace ccode=342 if country=="Serbia and Montenegro"&year>2002&year<2007
replace ccode = 341 if country=="Montenegro"
replace ccode=342 if country=="Serbia"
*Vietnam
replace ccode=818 if ccode==816
*South Sudan
replace ccode=626 if country=="South Sudan"
replace ccode=626 if country=="South Sudan"
drop if ccode==.
*note this drops various colonies that were not sovereign states.
duplicates drop
gen cyear=.
replace cyear=ccode*10000+year
sort cyear
gen lied6 = 0
replace lied6=1 if lexical_index==6
replace lied6=. if lexical_index==.
bys year: egen proplied6=mean(lied6)
label var proplied6 "proportion of countries for which LIED = 6"

gen lied5 = 0
replace lied5=1 if lexical_index==5|lexical_index==6
replace lied5=. if lexical_index==.
bys year: egen proplied5=mean(lied5)
label var proplied5 "proportion of countries for which LIED >= 5"
xtset ccode year
gen liedbkd=0
replace liedbkd=1 if l.lexical_index==6&lexical_index<6
replace liedbkd=. if l.lexical_index==.|lexical_index==.
bys year: egen mliedbkd = mean(liedbkd)
xtset ccode year
gen lmliedbkd = l.mliedbkd
gen l2mliedbkd = l2.mliedbkd
gen ldmliedbkd=lmliedbkd-l2mliedbkd

sort cyear
save "lied_v6wk.dta", replace


**********************************************************************************************************************
*Get SWIID V6.2 data from https://dataverse.harvard.edu/file.xhtml?fileId=3136167&version=RELEASED&version=.0
*Generate averages of inequality imputed estimates
**********************************************************************************************************************
use "swiid9_0.dta", clear
egen avginidisp = rowmean(_1_gini_disp _2_gini_disp _3_gini_disp _4_gini_disp _5_gini_disp _6_gini_disp _7_gini_disp _8_gini_disp _9_gini_disp _10_gini_disp _11_gini_disp _12_gini_disp _13_gini_disp _14_gini_disp _15_gini_disp _16_gini_disp _17_gini_disp _18_gini_disp _19_gini_disp _20_gini_disp _21_gini_disp _22_gini_disp _23_gini_disp _24_gini_disp _25_gini_disp _26_gini_disp _27_gini_disp _28_gini_disp _29_gini_disp _30_gini_disp _31_gini_disp _32_gini_disp _33_gini_disp _34_gini_disp _35_gini_disp _36_gini_disp _37_gini_disp _38_gini_disp _39_gini_disp _40_gini_disp _41_gini_disp _42_gini_disp _43_gini_disp _44_gini_disp _45_gini_disp _46_gini_disp _47_gini_disp _48_gini_disp _49_gini_disp _50_gini_disp _51_gini_disp _52_gini_disp _53_gini_disp _54_gini_disp _55_gini_disp _56_gini_disp _57_gini_disp _58_gini_disp _59_gini_disp _60_gini_disp _61_gini_disp _62_gini_disp _63_gini_disp _64_gini_disp _65_gini_disp _66_gini_disp _67_gini_disp _68_gini_disp _69_gini_disp _70_gini_disp _71_gini_disp _72_gini_disp _73_gini_disp _74_gini_disp _75_gini_disp _76_gini_disp _77_gini_disp _78_gini_disp _79_gini_disp _80_gini_disp _81_gini_disp _82_gini_disp _83_gini_disp _84_gini_disp _85_gini_disp _86_gini_disp _87_gini_disp _88_gini_disp _89_gini_disp _90_gini_disp _91_gini_disp _92_gini_disp _93_gini_disp _94_gini_disp _95_gini_disp _96_gini_disp _97_gini_disp _98_gini_disp _99_gini_disp _100_gini_disp)
egen avginimkt = rowmean(_1_gini_mkt _2_gini_mkt _3_gini_mkt _4_gini_mkt _5_gini_mkt _6_gini_mkt _7_gini_mkt _8_gini_mkt _9_gini_mkt _10_gini_mkt _11_gini_mkt _12_gini_mkt _13_gini_mkt _14_gini_mkt _15_gini_mkt _16_gini_mkt _17_gini_mkt _18_gini_mkt _19_gini_mkt _20_gini_mkt _21_gini_mkt _22_gini_mkt _23_gini_mkt _24_gini_mkt _25_gini_mkt _26_gini_mkt _27_gini_mkt _28_gini_mkt _29_gini_mkt _30_gini_mkt _31_gini_mkt _32_gini_mkt _33_gini_mkt _34_gini_mkt _35_gini_mkt _36_gini_mkt _37_gini_mkt _38_gini_mkt _39_gini_mkt _40_gini_mkt _41_gini_mkt _42_gini_mkt _43_gini_mkt _44_gini_mkt _45_gini_mkt _46_gini_mkt _47_gini_mkt _48_gini_mkt _49_gini_mkt _50_gini_mkt _51_gini_mkt _52_gini_mkt _53_gini_mkt _54_gini_mkt _55_gini_mkt _56_gini_mkt _57_gini_mkt _58_gini_mkt _59_gini_mkt _60_gini_mkt _61_gini_mkt _62_gini_mkt _63_gini_mkt _64_gini_mkt _65_gini_mkt _66_gini_mkt _67_gini_mkt _68_gini_mkt _69_gini_mkt _70_gini_mkt _71_gini_mkt _72_gini_mkt _73_gini_mkt _74_gini_mkt _75_gini_mkt _76_gini_mkt _77_gini_mkt _78_gini_mkt _79_gini_mkt _80_gini_mkt _81_gini_mkt _82_gini_mkt _83_gini_mkt _84_gini_mkt _85_gini_mkt _86_gini_mkt _87_gini_mkt _88_gini_mkt _89_gini_mkt _90_gini_mkt _91_gini_mkt _92_gini_mkt _93_gini_mkt _94_gini_mkt _95_gini_mkt _96_gini_mkt _97_gini_mkt _98_gini_mkt _99_gini_mkt _100_gini_mkt)
mi unset
keep country year avginidisp avginimkt
gen ccode=.
replace ccode = 100 if country=="Colombia"
replace ccode = 1001 if country=="Hong Kong"|country=="China: Hong Kong SAR"|country=="Hong Kong SAR"|country=="Hong Kong SAR, China"|country=="China, Hong Kong SAR"|country=="Hong Kong, China"
replace ccode = 1002 if country=="Puerto Rico"
replace ccode = 1005 if country=="Palestine"|country=="State of Palestine"|country=="Palestinian Territories"|country=="West Bank and Gaza Strip"|country=="West Bank and Gaza"|country=="Israeli-Occupied Territories and Palestinian Authority"|country=="Palestinian Authority"|country=="West Bank"|country=="Palestine, State of"
replace ccode = 101 if country=="Venezuela"|country=="Venezuela (Bolivarian Republic of)"|country=="Venezuela, RB"|country=="Venezuela, Bolivarian Republic of"
replace ccode = 110 if country=="Guyana"
replace ccode = 115 if country=="Suriname"
replace ccode = 130 if country=="Ecuador"
replace ccode = 135 if country=="Peru"
replace ccode = 140 if country=="Brazil"
replace ccode = 145 if country=="Bolivia"|country=="Bolivia (Plurinational State of)"|country=="Bolivia, Plurinational State of"
replace ccode = 150 if country=="Paraguay"
replace ccode = 155 if country=="Chile"
replace ccode = 160 if country=="Argentina"
replace ccode = 165 if country=="Uruguay"
replace ccode = 166 if country=="Faeroe Islands"
replace ccode = 2 if country=="United States of America"|country=="United States"|country=="USA"|country=="US"
replace ccode = 20 if country=="Canada"
replace ccode = 200 if country=="Great Britain"|country=="United Kingdom"|country=="UK"
replace ccode = 201 if country=="Channel Islands"
replace ccode = 202 if country=="Isle of Man"
replace ccode = 205 if country=="Ireland"
replace ccode = 210 if country=="Netherlands"
replace ccode = 211 if country=="Belgium"
replace ccode = 212 if country=="Luxembourg"
replace ccode = 220 if country=="France"
replace ccode = 221 if country=="Monaco"
replace ccode = 223 if country=="Liechtenstein"
replace ccode = 225 if country=="Switzerland"
replace ccode = 230 if country=="Spain"
replace ccode = 232 if country=="Andorra"
replace ccode = 235 if country=="Portugal"
replace ccode = 255 if country=="FRG/Germany"|country=="West Germany"|country=="Germany, West"|country=="Germany"|country=="West Germany (FRG)"|country=="German Federal Republic"
replace ccode = 265 if country=="GDR"|country=="East Germany"|country=="Germany, East"|country=="East Germany (GDR)"|country=="Germany East"|country=="German Democratic Republic"
replace ccode = 290 if country=="Poland"
replace ccode = 305 if country=="Austria"
replace ccode = 31 if country=="Bahamas"
replace ccode = 31 if country=="Bahamas"|country=="Bahamas, The"|country=="The Bahamas"
replace ccode = 310 if country=="Hungary"
replace ccode = 316 if country=="Czech Rep."|country=="Czech Republic"|country=="Czechia"
replace ccode = 316 if country=="Czechoslovakia"
replace ccode = 317 if country=="Slovak Republic"|country=="Slovakia"|country=="Slovak Rep."
replace ccode = 32 if country=="Aruba"
replace ccode = 325 if country=="Italy"
replace ccode = 33 if country=="Bermuda"
replace ccode = 331 if country=="San Marino"
replace ccode = 338 if country=="Malta"
replace ccode = 339 if country=="Albania"
replace ccode = 341 if country=="Montenegro"
replace ccode = 342 if country=="Serbia and Montenegro"|country=="Serbia-Montenegro"
replace ccode = 342 if country=="Serbia"
replace ccode = 342 if country=="Yugoslavia"
replace ccode = 343 if country=="F.Y.R. Macedonia"|country=="Macedonia, FYR"|country=="FYR Macedonia"|country=="TFYR of Macedonia"|country=="Macedonia"|country=="The former Yugoslav Republic of Macedonia"|country=="Macedonia, the former Yugoslav Republic of"|country=="TFYR Macedonia"|country=="Macedonia (FYROM)"
replace ccode = 344 if country=="Croatia"
replace ccode = 346 if country=="Bosnia and Herzegovina"|country=="Bosnia"|country=="Bosnia-Herz"|country=="Bosnia-Herzegovina"|country=="Bosnia & Herzegovina"
replace ccode = 347 if country=="Kosovo"
replace ccode = 349 if country=="Slovenia"
replace ccode = 350 if country=="Greece"
replace ccode = 352 if country=="Cyprus"|country=="Cyprus (Greek)"
replace ccode = 355 if country=="Bulgaria"
replace ccode = 359 if country=="Moldova"|country=="Republic of Moldova"|country=="Moldova, Republic of"
replace ccode = 360 if country=="Romania"
replace ccode = 365 if country=="Russia"|country=="Russian Federation"|country=="Russia/USSR"|country=="USSR"|country=="Soviet Union"
replace ccode = 366 if country=="Estonia"
replace ccode = 367 if country=="Latvia"
replace ccode = 368 if country=="Lithuania"
replace ccode = 369 if country=="Ukraine"
replace ccode = 370 if country=="Belarus"
replace ccode = 371 if country=="Armenia"
replace ccode = 372 if country=="Georgia"
replace ccode = 373 if country=="Azerbaijan"
replace ccode = 375 if country=="Finland"
replace ccode = 380 if country=="Sweden"
replace ccode = 385 if country=="Norway"
replace ccode = 390 if country=="Denmark"
replace ccode = 395 if country=="Iceland"
replace ccode = 396 if country=="Greenland"
replace ccode = 40 if country=="Cuba"
replace ccode = 402 if country=="C. Verde Is."|country=="Cabo Verde"|country=="Cape Verde"
replace ccode = 403 if country=="Sao Tome and Principe"|country=="São Tomé and Príncipe"|country=="Sao Tome"|country=="S. Tomé & Principe"
replace ccode = 404 if country=="Guinea-Bissau"|country=="Guinea Bissau"
replace ccode = 41 if country=="Haiti"
replace ccode = 411 if country=="Eq. Guinea"|country=="Equatorial Guinea"
replace ccode = 42 if country=="Dom. Rep."|country=="Dominican Rep."|country=="Dominican Republic"|country=="Dominican Rep"
replace ccode = 420 if country=="Gambia"|country=="Gambia, The"|country=="The Gambia"
replace ccode = 432 if country=="Mali"
replace ccode = 433 if country=="Senegal"
replace ccode = 434 if country=="Benin"
replace ccode = 435 if country=="Mauritania"
replace ccode = 436 if country=="Niger"
replace ccode = 437 if country=="C?te d'Ivoire"|country=="Cote d`Ivoire"|country=="Côte d´Ivoire"|country=="Côte d’Ivoire"|country=="Côte d'Ivoire"|country=="Cote d'Ivoire"|country=="Ivory Coast (Cote d'Ivoire)"|country=="Ivory Coast"|country=="Côte d'Ivoire"|country=="Côte D'Ivoire"
replace ccode = 438 if country=="Guinea"
replace ccode = 439 if country=="Burkina Faso"
replace ccode = 450 if country=="Liberia"
replace ccode = 451 if country=="Sierra Leone"
replace ccode = 452 if country=="Ghana"
replace ccode = 461 if country=="Togo"
replace ccode = 471 if country=="Cameroon"
replace ccode = 475 if country=="Nigeria"
replace ccode = 481 if country=="Gabon"
replace ccode = 482 if country=="Cent. Af. Rep."
replace ccode = 482 if country=="Central Afr. Rep."|country=="Cen African Rep"|country=="Central African Rep."
replace ccode = 482 if country=="Central African Republic"
replace ccode = 483 if country=="Chad"
replace ccode = 484 if country=="Congo (Brazzaville)"
replace ccode = 484 if country=="Congo Brazzaville"|country=="Congo-Brz"
replace ccode = 484 if country=="Congo Republic"
replace ccode = 484 if country=="Congo"
replace ccode = 484 if country=="Congo, Rep. Of"
replace ccode = 484 if country=="Congo, Rep."|country=="Congo-Brazzaville"|country=="Republic of Congo"|country=="Congo, Republic"|country=="Republic of the Congo"
replace ccode = 484 if country=="Congo, Republic of"|country=="Congo, Republic of (Brazzaville)"
replace ccode = 484 if country=="Congo, Republicof"|country=="Congo (Rep.)"
replace ccode = 490 if country=="Congo (Democratic Republic of the)"|country=="Congo, the Democratic Republic of the"|country=="Congo (Dem. Rep.)"|country=="Congo, D.R."
replace ccode = 490 if country=="Congo (DRC)"|country=="Congo, Democratic Republic of (Kinshasa)"|country=="Zaire (Democ Republic Congo)"|country=="Congo/Zaire"
replace ccode = 490 if country=="Congo (Kinshasa)"|country=="Congo-Kinshasa"
replace ccode = 490 if country=="Congo, Dem. R."
replace ccode = 490 if country=="Congo, Dem. Rep."|country=="Congo, Democratic Republic"|country=="Democratic Republic of Congo"|country=="Zaire"
replace ccode = 490 if country=="Congo, Democratic Republic of"|country=="Congo, Dem. Rep."|country=="D.R. of the Congo"
replace ccode = 490 if country=="Democratic Republic of the Congo"
replace ccode = 500 if country=="Uganda"
replace ccode = 501 if country=="Kenya"
replace ccode = 51 if country=="Jamaica"
replace ccode = 510 if country=="Tanzania (United Republic of)"|country=="U.R. of Tanzania: Mainland"|country=="Tanzania, United Republic of"
replace ccode = 510 if country=="Tanzania"|country=="United Republic of Tanzania"
replace ccode = 516 if country=="Burundi"
replace ccode = 517 if country=="Rwanda"
replace ccode = 52 if country=="Trinidad & Tob."
replace ccode = 52 if country=="Trinidad & Tobago"
replace ccode = 52 if country=="Trinidad and Tobago"
replace ccode = 52 if country=="Trinidad-Tobago"
replace ccode = 520 if country=="Somalia"
replace ccode = 522 if country=="Djibouti"
replace ccode = 53 if country=="Barbados"
replace ccode = 530 if country=="Ethiopia"
replace ccode = 531 if country=="Eritrea"
replace ccode = 54 if country=="Dominica"
replace ccode = 540 if country=="Angola"
replace ccode = 541 if country=="Mozambique"
replace ccode = 55 if country=="Grenada"
replace ccode = 551 if country=="Zambia"
replace ccode = 552 if country=="Zimbabwe"|country=="Rhodesia"
replace ccode = 553 if country=="Malawi"
replace ccode = 56 if country=="Saint Lucia"
replace ccode = 56 if country=="St. Lucia"
replace ccode = 560 if country=="S. Africa"
replace ccode = 560 if country=="South Africa"
replace ccode = 565 if country=="Namibia"
replace ccode = 57 if country=="Saint Vincent and the Grenadines"|country=="St Vincent"
replace ccode = 57 if country=="St. Vincent & Grenadines"
replace ccode = 57 if country=="St. Vincent and Grenadines"
replace ccode = 57 if country=="St. Vincent and the Grenadines"
replace ccode = 570 if country=="Lesotho"
replace ccode = 571 if country=="Botswana"
replace ccode = 572 if country=="Swaziland"
replace ccode = 58 if country=="Antigua and Barbuda"|country=="Antigua & Barbuda"
replace ccode = 580 if country=="Madagascar"
replace ccode = 581 if country=="Comoro Is."
replace ccode = 581 if country=="Comoros"
replace ccode = 59 if country=="Cayman Islands"
replace ccode = 590 if country=="Mauritius"
replace ccode = 591 if country=="Seychelles"
replace ccode = 60 if country=="Saint Kitts & Nevis"|country=="Saint Kitts and Nevis"|country=="St Kitts"
replace ccode = 60 if country=="St. Kitts and Nevis"
replace ccode = 60 if country=="St.Kitts & Nevis"
replace ccode = 600 if country=="Morocco"
replace ccode = 61 if country=="Curacao"
replace ccode = 615 if country=="Algeria"
replace ccode = 616 if country=="Tunisia"
replace ccode = 62 if country=="Turks and Caicos Islands"
replace ccode = 620 if country=="Libya"
replace ccode = 625 if country=="Sudan"|country=="Sudan (Former)"
replace ccode = 626 if country=="South Sudan"
replace ccode = 63 if country=="Virgin Islands (U.S.)"
replace ccode = 630 if country=="Iran (Islamic Republic of)"|country=="Iran, Islamic Republic of"
replace ccode = 630 if country=="Iran"|country=="Iran (I.R.)"
replace ccode = 630 if country=="Iran"
replace ccode = 630 if country=="Iran, Islamic Rep."|country=="Islamic Republic of Iran"
replace ccode = 640 if country=="Turkey"
replace ccode = 645 if country=="Iraq"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt, Arab Rep."
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syrian Arab Republic"
replace ccode = 660 if country=="Lebanon"
replace ccode = 663 if country=="Jordan"
replace ccode = 666 if country=="Israel"|country=="Israel and Occupied Territories**"|country=="Israel in pre-1967 borders"
replace ccode = 670 if country=="Saudi Arabia"
replace ccode = 678 if country=="Yemen Arab Republic"|country=="Yemen (A.R.)"|country=="North Yemen"|country=="Yemen (AR)"
replace ccode = 679 if country=="Yemen, Rep."|country=="Yemen"|country=="Yemen, Republic of"
replace ccode = 680 if country=="Yemen (PDR)"|country=="South Yemen"|country=="Yemen, South"|country=="SouthYemen"|country=="Yemen People's Republic"|country=="Yemen  (PDR)"|country=="Yemen South"
replace ccode = 690 if country=="Kuwait"
replace ccode = 692 if country=="Bahrain"
replace ccode = 694 if country=="Qatar"
replace ccode = 696 if country=="Unit. Arab Em."
replace ccode = 696 if country=="United Arab Emirates"|country=="UAE"|country=="United Arab Emirates#"
replace ccode = 698 if country=="Oman"
replace ccode = 70 if country=="Mexico"
replace ccode = 700 if country=="Afghanistan"
replace ccode = 701 if country=="Turkmenistan"
replace ccode = 702 if country=="Tajikistan"
replace ccode = 703 if country=="Kyrgyz Republic"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 704 if country=="Uzbekistan"
replace ccode = 705 if country=="Kazakhstan"
replace ccode = 710 if country=="China (PRC)"
replace ccode = 710 if country=="China"
replace ccode = 710 if country=="China, P. R."
replace ccode = 710 if country=="PRC"
replace ccode = 711 if country=="China: Macao SAR"|country=="Macao, China"
replace ccode = 711 if country=="Macao SAR, China"|country=="China, Macao SAR"
replace ccode = 711 if country=="Macao"|country=="Macau"
replace ccode = 712 if country=="Mongolia"
replace ccode = 713 if country=="Taiwan (ROC)"|country=="Taiwan Province of China"|country=="Taiwan, China"|country=="Taiwan, Province of China"
replace ccode = 713 if country=="Taiwan"
replace ccode = 713 if country=="Taiwan"
replace ccode = 731 if country=="Korea (North)"|country=="Korea North"|country=="Korea, Democratic People's Republic of"
replace ccode = 731 if country=="Korea, Dem. Rep."|country=="Democratic People's Republic of Korea"|country=="D.P.R. Korea"
replace ccode = 731 if country=="North Korea"|country=="Korea, North"
replace ccode = 731 if country=="PRK"|country=="North Korea (Democratc People's Republic of Korea)"
replace ccode = 732 if country=="Korea (South)"|country=="Korea South"
replace ccode = 732 if country=="Korea, Rep."|country=="Korea"|country=="Korea (Rep.)"
replace ccode = 732 if country=="Korea, Republic of"
replace ccode = 732 if country=="Korea, South"
replace ccode = 732 if country=="ROK"
replace ccode = 732 if country=="South Korea"|country=="Korea, South"|country=="Republic of Korea"|country=="South Korea (Republic of Korea)"
replace ccode = 740 if country=="Japan"
replace ccode = 750 if country=="India"
replace ccode = 760 if country=="Bhutan"
replace ccode = 770 if country=="Pakistan"
replace ccode = 771 if country=="Bangladesh"
replace ccode = 775 if country=="Burma (Myanmar)"
replace ccode = 775 if country=="Burma"
replace ccode = 775 if country=="Myanmar"|country=="Myanmar (Burma)"
replace ccode = 780 if country=="Sri Lanka"
replace ccode = 781 if country=="Maldives"
replace ccode = 790 if country=="Nepal"
replace ccode = 80 if country=="Belize"
replace ccode = 800 if country=="Thailand"
replace ccode = 811 if country=="Cambodia"
replace ccode = 812 if country=="Lao PDR"|country=="Lao P.D.R."
replace ccode = 812 if country=="Lao People's Democratic Republic"
replace ccode = 812 if country=="Lao"|country=="Lao People's DR"
replace ccode = 812 if country=="Laos"
replace ccode = 812 if country=="Laos"
replace ccode = 817 if country=="Vietnam, South"|country=="South Vietnam"|country=="Vietnam South"
replace ccode = 818 if country=="Viet Nam"|country=="Vietnam, Socialist Republic of"
replace ccode = 818 if country=="Vietnam"
replace ccode = 820 if country=="Malaysia"
replace ccode = 830 if country=="Singapore"
replace ccode = 835 if country=="Brunei Darussalam"
replace ccode = 835 if country=="Brunei"
replace ccode = 835 if country=="Brunei"
replace ccode = 840 if country=="Philippines"|country=="Philippines +"
replace ccode = 850 if country=="Indonesia"
replace ccode = 860 if country=="East Timor"|country=="East Timor (Timor L'este)"
replace ccode = 860 if country=="Timor-Leste"|country=="Timor Leste"
replace ccode = 90 if country=="Guatemala"
replace ccode = 900 if country=="Australia"
replace ccode = 91 if country=="Honduras"
replace ccode = 910 if country=="P.N. Guinea"
replace ccode = 910 if country=="Papua New Guinea"|country=="P.N. Guinea"|country=="P. N. Guinea"
replace ccode = 92 if country=="El Salvador"
replace ccode = 920 if country=="New Zealand"
replace ccode = 93 if country=="Nicaragua"
replace ccode = 935 if country=="Vanuatu"
replace ccode = 94 if country=="Costa Rica"
replace ccode = 940 if country=="Solomon Is."
replace ccode = 940 if country=="Solomon Islands"
replace ccode = 941 if country=="Northern Mariana Islands"
replace ccode = 946 if country=="Kiribati"
replace ccode = 947 if country=="Tuvalu"
replace ccode = 95 if country=="Panama"
replace ccode = 950 if country=="Fiji"
replace ccode = 951 if country=="French Polynesia"
replace ccode = 952 if country=="New Caledonia"
replace ccode = 955 if country=="Tonga"
replace ccode = 983 if country=="Marshall Islands"
replace ccode = 986 if country=="Palau"
replace ccode = 987 if country=="Micronesia (Federated States of)"|country=="Micronesia (FS of)"|country=="Micronesia, Federated States of"
replace ccode = 987 if country=="Micronesia"
replace ccode = 987 if country=="Micronesia, Fed. Sts."
replace ccode = 990 if country=="Samoa"|country=="W. Samoa"|country=="Western Samoa"
replace ccode = 991 if country=="Guam"
replace ccode = 992 if country=="Nauru"
replace ccode = 993 if country=="Niue"
replace ccode = 994 if country=="Cook Islands"
replace ccode = 995 if country=="American Samoa"
replace ccode=35 if country=="Anguilla"
replace ccode=36 if country=="Curaçao"
replace ccode=37 if country=="Montserrat"
replace ccode=38 if country=="Sint Maarten (Dutch part)"
replace ccode=39 if country=="British Virgin Islands"
replace ccode=521 if country=="Somaliland"|country=="Somaliland Region"
replace ccode=353 if country=="Northern Cyprus"
drop if country=="Czech Republic"&year==1988
drop if country=="Czech Republic"&year==1989
drop if country=="Russia"&year==1988
drop if country=="Russia"&year==1989
drop if country=="Russia"&year==1990
gen cyear=ccode*10000+year
sort cyear
save "swiid9_0work.dta", replace



******************************************************************************
* DATABASE OF POLITICAL INSTITUTIONS (DPI)
******************************************************************************
use "DPI2020.dta", clear
gen ccode=.
replace ccode = 100 if country=="Colombia"
replace ccode = 1001 if country=="Hong Kong"|country=="China: Hong Kong SAR"|country=="Hong Kong SAR"|country=="Hong Kong SAR, China"|country=="China, Hong Kong SAR"|country=="Hong Kong, China"
replace ccode = 1002 if country=="Puerto Rico"
replace ccode = 1005 if country=="Palestine"|country=="State of Palestine"|country=="Palestinian Territories"|country=="West Bank and Gaza Strip"|country=="West Bank and Gaza"|country=="Israeli-Occupied Territories and Palestinian Authority"|country=="Palestinian Authority"|country=="West Bank"|country=="Palestine, State of"
replace ccode = 101 if country=="Venezuela"|country=="Venezuela (Bolivarian Republic of)"|country=="Venezuela, RB"|country=="Venezuela, Bolivarian Republic of"
replace ccode = 110 if country=="Guyana"
replace ccode = 115 if country=="Suriname"
replace ccode = 130 if country=="Ecuador"
replace ccode = 135 if country=="Peru"
replace ccode = 140 if country=="Brazil"
replace ccode = 145 if country=="Bolivia"|country=="Bolivia (Plurinational State of)"|country=="Bolivia, Plurinational State of"
replace ccode = 150 if country=="Paraguay"
replace ccode = 155 if country=="Chile"
replace ccode = 160 if country=="Argentina"
replace ccode = 165 if country=="Uruguay"
replace ccode = 166 if country=="Faeroe Islands"
replace ccode = 2 if country=="United States of America"|country=="United States"|country=="USA"|country=="US"
replace ccode = 20 if country=="Canada"
replace ccode = 200 if country=="Great Britain"|country=="United Kingdom"|country=="UK"
replace ccode = 201 if country=="Channel Islands"
replace ccode = 202 if country=="Isle of Man"
replace ccode = 205 if country=="Ireland"
replace ccode = 210 if country=="Netherlands"
replace ccode = 211 if country=="Belgium"
replace ccode = 212 if country=="Luxembourg"
replace ccode = 220 if country=="France"
replace ccode = 221 if country=="Monaco"
replace ccode = 223 if country=="Liechtenstein"
replace ccode = 225 if country=="Switzerland"
replace ccode = 230 if country=="Spain"
replace ccode = 232 if country=="Andorra"
replace ccode = 235 if country=="Portugal"
replace ccode = 255 if country=="FRG/Germany"|country=="West Germany"|country=="Germany, West"|country=="Germany"|country=="West Germany (FRG)"|country=="German Federal Republic"|country=="Germany West"
replace ccode = 265 if country=="GDR"|country=="East Germany"|country=="Germany, East"|country=="East Germany (GDR)"|country=="Germany East"|country=="German Democratic Republic"
replace ccode = 290 if country=="Poland"
replace ccode = 305 if country=="Austria"
replace ccode = 31 if country=="Bahamas"
replace ccode = 31 if country=="Bahamas"|country=="Bahamas, The"|country=="The Bahamas"
replace ccode = 310 if country=="Hungary"
replace ccode = 316 if country=="Czech Rep."|country=="Czech Republic"|country=="Czechia"
replace ccode = 316 if country=="Czechoslovakia"
replace ccode = 317 if country=="Slovak Republic"|country=="Slovakia"|country=="Slovak Rep."
replace ccode = 32 if country=="Aruba"
replace ccode = 325 if country=="Italy"
replace ccode = 33 if country=="Bermuda"
replace ccode = 331 if country=="San Marino"
replace ccode = 338 if country=="Malta"
replace ccode = 339 if country=="Albania"
replace ccode = 341 if country=="Montenegro"
replace ccode = 342 if country=="Serbia and Montenegro"|country=="Serbia-Montenegro"
replace ccode = 342 if country=="Serbia"
replace ccode = 342 if country=="Yugoslavia"
replace ccode = 343 if country=="F.Y.R. Macedonia"|country=="Macedonia, FYR"|country=="FYR Macedonia"|country=="TFYR of Macedonia"|country=="Macedonia"|country=="The former Yugoslav Republic of Macedonia"|country=="Macedonia, the former Yugoslav Republic of"|country=="TFYR Macedonia"|country=="North Macedonia"
replace ccode = 344 if country=="Croatia"
replace ccode = 346 if country=="Bosnia and Herzegovina"|country=="Bosnia"|country=="Bosnia-Herz"|country=="Bosnia-Herzegovina"|country=="Bosnia & Herzegovina"
replace ccode = 347 if country=="Kosovo"
replace ccode = 349 if country=="Slovenia"
replace ccode = 350 if country=="Greece"
replace ccode = 352 if country=="Cyprus"|country=="Cyprus (Greek)"
replace ccode = 355 if country=="Bulgaria"
replace ccode = 359 if country=="Moldova"|country=="Republic of Moldova"|country=="Moldova, Republic of"
replace ccode = 360 if country=="Romania"
replace ccode = 365 if country=="Russia"|country=="Russian Federation"|country=="Russia/USSR"|country=="USSR"|country=="Soviet Union"
replace ccode = 366 if country=="Estonia"
replace ccode = 367 if country=="Latvia"
replace ccode = 368 if country=="Lithuania"
replace ccode = 369 if country=="Ukraine"
replace ccode = 370 if country=="Belarus"
replace ccode = 371 if country=="Armenia"
replace ccode = 372 if country=="Georgia"
replace ccode = 373 if country=="Azerbaijan"
replace ccode = 375 if country=="Finland"
replace ccode = 380 if country=="Sweden"
replace ccode = 385 if country=="Norway"
replace ccode = 390 if country=="Denmark"
replace ccode = 395 if country=="Iceland"
replace ccode = 396 if country=="Greenland"
replace ccode = 40 if country=="Cuba"
replace ccode = 402 if country=="C. Verde Is."|country=="Cabo Verde"|country=="Cape Verde"
replace ccode = 403 if country=="Sao Tome and Principe"|country=="São Tomé and Príncipe"|country=="Sao Tome"|country=="S. Tomé & Principe"
replace ccode = 404 if country=="Guinea-Bissau"|country=="Guinea Bissau"
replace ccode = 41 if country=="Haiti"
replace ccode = 411 if country=="Eq. Guinea"|country=="Equatorial Guinea"
replace ccode = 42 if country=="Dom. Rep."|country=="Dominican Rep."|country=="Dominican Republic"|country=="Dominican Rep"
replace ccode = 420 if country=="Gambia"|country=="Gambia, The"|country=="The Gambia"
replace ccode = 432 if country=="Mali"
replace ccode = 433 if country=="Senegal"
replace ccode = 434 if country=="Benin"
replace ccode = 435 if country=="Mauritania"
replace ccode = 436 if country=="Niger"
replace ccode = 437 if country=="C?te d'Ivoire"|country=="Cote d`Ivoire"|country=="Côte d´Ivoire"|country=="Côte d’Ivoire"|country=="Côte d'Ivoire"|country=="Cote d'Ivoire"|country=="Ivory Coast (Cote d'Ivoire)"|country=="Ivory Coast"|country=="Côte d'Ivoire"|country=="Côte D'Ivoire"|country=="Cote D'Ivoire"
replace ccode = 438 if country=="Guinea"
replace ccode = 439 if country=="Burkina Faso"
replace ccode = 450 if country=="Liberia"
replace ccode = 451 if country=="Sierra Leone"
replace ccode = 452 if country=="Ghana"
replace ccode = 461 if country=="Togo"
replace ccode = 471 if country=="Cameroon"
replace ccode = 475 if country=="Nigeria"
replace ccode = 481 if country=="Gabon"
replace ccode = 482 if country=="Cent. Af. Rep."
replace ccode = 482 if country=="Central Afr. Rep."|country=="Cen African Rep"|country=="Central African Rep."
replace ccode = 482 if country=="Central African Republic"
replace ccode = 483 if country=="Chad"
replace ccode = 484 if country=="Congo (Brazzaville)"
replace ccode = 484 if country=="Congo Brazzaville"|country=="Congo-Brz"
replace ccode = 484 if country=="Congo Republic"
replace ccode = 484 if country=="Congo"
replace ccode = 484 if country=="Congo, Rep. Of"
replace ccode = 484 if country=="Congo, Rep."|country=="Congo-Brazzaville"|country=="Republic of Congo"|country=="Congo, Republic"|country=="Republic of the Congo"
replace ccode = 484 if country=="Congo, Republic of"|country=="Congo, Republic of (Brazzaville)"
replace ccode = 484 if country=="Congo, Republicof"|country=="Congo (Rep.)"
replace ccode = 490 if country=="Congo (Democratic Republic of the)"|country=="Congo, the Democratic Republic of the"|country=="Congo (Dem. Rep.)"|country==" Congo, D.R."|country=="Congo Kinshasa"
replace ccode = 490 if country=="Congo (DRC)"|country=="Congo, Democratic Republic of (Kinshasa)"|country=="Zaire (Democ Republic Congo)"|country=="Congo/Zaire"
replace ccode = 490 if country=="Congo (Kinshasa)"|country=="Congo-Kinshasa"
replace ccode = 490 if country=="Congo, D.R."
replace ccode = 490 if country=="Congo, Dem. Rep."|country=="Congo, Democratic Republic"|country=="Democratic Republic of Congo"|country=="Zaire"
replace ccode = 490 if country=="Congo, Democratic Republic of"|country=="Congo, Dem. Rep."|country=="D.R. of the Congo"
replace ccode = 490 if country=="Democratic Republic of the Congo"
replace ccode = 500 if country=="Uganda"
replace ccode = 501 if country=="Kenya"
replace ccode = 51 if country=="Jamaica"
replace ccode = 510 if country=="Tanzania (United Republic of)"|country=="U.R. of Tanzania: Mainland"|country=="Tanzania, United Republic of"
replace ccode = 510 if country=="Tanzania"|country=="United Republic of Tanzania"
replace ccode = 516 if country=="Burundi"
replace ccode = 517 if country=="Rwanda"
replace ccode = 52 if country=="Trinidad & Tob."
replace ccode = 52 if country=="Trinidad & Tobago"
replace ccode = 52 if country=="Trinidad and Tobago"
replace ccode = 52 if country=="Trinidad-Tobago"
replace ccode = 520 if country=="Somalia"
replace ccode = 522 if country=="Djibouti"
replace ccode = 53 if country=="Barbados"
replace ccode = 530 if country=="Ethiopia"
replace ccode = 531 if country=="Eritrea"
replace ccode = 54 if country=="Dominica"
replace ccode = 540 if country=="Angola"
replace ccode = 541 if country=="Mozambique"
replace ccode = 55 if country=="Grenada"
replace ccode = 551 if country=="Zambia"
replace ccode = 552 if country=="Zimbabwe"|country=="Rhodesia"
replace ccode = 553 if country=="Malawi"
replace ccode = 56 if country=="Saint Lucia"
replace ccode = 56 if country=="St. Lucia"
replace ccode = 560 if country=="S. Africa"
replace ccode = 560 if country=="South Africa"
replace ccode = 565 if country=="Namibia"
replace ccode = 57 if country=="Saint Vincent and the Grenadines"|country=="St Vincent"
replace ccode = 57 if country=="St. Vincent & Grenadines"
replace ccode = 57 if country=="St. Vincent and Grenadines"
replace ccode = 57 if country=="St. Vincent and the Grenadines"
replace ccode = 570 if country=="Lesotho"
replace ccode = 571 if country=="Botswana"
replace ccode = 572 if country=="Swaziland"|country=="eSwatini"
replace ccode = 58 if country=="Antigua and Barbuda"|country=="Antigua & Barbuda"
replace ccode = 580 if country=="Madagascar"
replace ccode = 581 if country=="Comoro Is."
replace ccode = 581 if country=="Comoros"
replace ccode = 59 if country=="Cayman Islands"
replace ccode = 590 if country=="Mauritius"
replace ccode = 591 if country=="Seychelles"
replace ccode = 60 if country=="Saint Kitts & Nevis"|country=="Saint Kitts and Nevis"|country=="St Kitts"
replace ccode = 60 if country=="St. Kitts and Nevis"
replace ccode = 60 if country=="St.Kitts & Nevis"
replace ccode = 600 if country=="Morocco"
replace ccode = 61 if country=="Curacao"
replace ccode = 615 if country=="Algeria"
replace ccode = 616 if country=="Tunisia"
replace ccode = 62 if country=="Turks and Caicos Islands"
replace ccode = 620 if country=="Libya"
replace ccode = 625 if country=="Sudan"|country=="Sudan (Former)"
replace ccode = 626 if country=="South Sudan"
replace ccode = 63 if country=="Virgin Islands (U.S.)"
replace ccode = 630 if country=="Iran (Islamic Republic of)"|country=="Iran, Islamic Republic of"
replace ccode = 630 if country=="Iran"|country=="Iran (I.R.)"
replace ccode = 630 if country=="Iran"
replace ccode = 630 if country=="Iran, Islamic Rep."|country=="Islamic Republic of Iran"
replace ccode = 640 if country=="Turkey"
replace ccode = 645 if country=="Iraq"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt, Arab Rep."
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syrian Arab Republic"
replace ccode = 660 if country=="Lebanon"
replace ccode = 663 if country=="Jordan"
replace ccode = 666 if country=="Israel"|country=="Israel and Occupied Territories**"|country=="Israel in pre-1967 borders"
replace ccode = 670 if country=="Saudi Arabia"
replace ccode = 678 if country=="Yemen Arab Republic"|country=="Yemen (A.R.)"|country=="North Yemen"|country=="Yemen (AR)"|country=="Yemen North"
replace ccode = 679 if country=="Yemen, Rep."|country=="Yemen"|country=="Yemen, Republic of"
replace ccode = 680 if country=="Yemen (PDR)"|country=="South Yemen"|country=="Yemen, South"|country=="SouthYemen"|country=="Yemen People's Republic"|country=="Yemen  (PDR)"|country=="Yemen South"
replace ccode = 690 if country=="Kuwait"
replace ccode = 692 if country=="Bahrain"
replace ccode = 694 if country=="Qatar"
replace ccode = 696 if country=="Unit. Arab Em."
replace ccode = 696 if country=="United Arab Emirates"|country=="UAE"|country=="United Arab Emirates#"
replace ccode = 698 if country=="Oman"
replace ccode = 70 if country=="Mexico"
replace ccode = 700 if country=="Afghanistan"
replace ccode = 701 if country=="Turkmenistan"
replace ccode = 702 if country=="Tajikistan"
replace ccode = 703 if country=="Kyrgyz Republic"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 704 if country=="Uzbekistan"
replace ccode = 705 if country=="Kazakhstan"
replace ccode = 710 if country=="China (PRC)"
replace ccode = 710 if country=="China"
replace ccode = 710 if country=="China, P. R."
replace ccode = 710 if country=="PRC"
replace ccode = 711 if country=="China: Macao SAR"|country=="Macao, China"
replace ccode = 711 if country=="Macao SAR, China"|country=="China, Macao SAR"
replace ccode = 711 if country=="Macao"|country=="Macau"
replace ccode = 712 if country=="Mongolia"
replace ccode = 713 if country=="Taiwan (ROC)"|country=="Taiwan Province of China"|country=="Taiwan, China"|country=="Taiwan, Province of China"
replace ccode = 713 if country=="Taiwan"
replace ccode = 713 if country=="Taiwan"
replace ccode = 731 if country=="Korea (North)"|country=="Korea North"|country=="Korea, Democratic People's Republic of"
replace ccode = 731 if country=="Korea, Dem. Rep."|country=="Democratic People's Republic of Korea"|country=="D.P.R. Korea"
replace ccode = 731 if country=="North Korea"|country=="Korea, North"
replace ccode = 731 if country=="PRK"|country=="North Korea (Democratc People's Republic of Korea)"
replace ccode = 732 if country=="Korea (South)"|country=="Korea South"
replace ccode = 732 if country=="Korea, Rep."|country=="Korea"|country=="Korea (Rep.)"
replace ccode = 732 if country=="Korea, Republic of"
replace ccode = 732 if country=="Korea, South"
replace ccode = 732 if country=="ROK"
replace ccode = 732 if country=="South Korea"|country=="Korea, South"|country=="Republic of Korea"|country=="South Korea (Republic of Korea)"
replace ccode = 740 if country=="Japan"
replace ccode = 750 if country=="India"
replace ccode = 760 if country=="Bhutan"
replace ccode = 770 if country=="Pakistan"
replace ccode = 771 if country=="Bangladesh"
replace ccode = 775 if country=="Burma (Myanmar)"
replace ccode = 775 if country=="Burma"
replace ccode = 775 if country=="Myanmar"|country=="Myanmar (Burma)"
replace ccode = 780 if country=="Sri Lanka"
replace ccode = 781 if country=="Maldives"
replace ccode = 790 if country=="Nepal"
replace ccode = 80 if country=="Belize"
replace ccode = 800 if country=="Thailand"
replace ccode = 811 if country=="Cambodia"
replace ccode = 812 if country=="Lao PDR"|country=="Lao P.D.R."
replace ccode = 812 if country=="Lao People's Democratic Republic"
replace ccode = 812 if country=="Lao"|country=="Lao People's DR"
replace ccode = 812 if country=="Laos"
replace ccode = 812 if country=="Laos"
replace ccode = 817 if country=="Vietnam, South"|country=="South Vietnam"|country=="Vietnam South"
replace ccode = 818 if country=="Viet Nam"|country=="Vietnam, Socialist Republic of"
replace ccode = 818 if country=="Vietnam"|country=="Vietnam North"
replace ccode = 820 if country=="Malaysia"
replace ccode = 830 if country=="Singapore"
replace ccode = 835 if country=="Brunei Darussalam"
replace ccode = 835 if country=="Brunei"
replace ccode = 835 if country=="Brunei"
replace ccode = 840 if country=="Philippines"|country=="Philippines +"
replace ccode = 850 if country=="Indonesia"
replace ccode = 860 if country=="East Timor"|country=="East Timor (Timor L'este)"
replace ccode = 860 if country=="Timor-Leste"|country=="Timor Leste"
replace ccode = 90 if country=="Guatemala"
replace ccode = 900 if country=="Australia"
replace ccode = 91 if country=="Honduras"
replace ccode = 910 if country=="P.N. Guinea"
replace ccode = 910 if country=="Papua New Guinea"|country=="P.N. Guinea"|country=="P. N. Guinea"
replace ccode = 92 if country=="El Salvador"
replace ccode = 920 if country=="New Zealand"
replace ccode = 93 if country=="Nicaragua"
replace ccode = 935 if country=="Vanuatu"
replace ccode = 94 if country=="Costa Rica"
replace ccode = 940 if country=="Solomon Is."
replace ccode = 940 if country=="Solomon Islands"
replace ccode = 941 if country=="Northern Mariana Islands"
replace ccode = 946 if country=="Kiribati"
replace ccode = 947 if country=="Tuvalu"
replace ccode = 95 if country=="Panama"
replace ccode = 950 if country=="Fiji"
replace ccode = 951 if country=="French Polynesia"
replace ccode = 952 if country=="New Caledonia"
replace ccode = 955 if country=="Tonga"
replace ccode = 983 if country=="Marshall Islands"
replace ccode = 986 if country=="Palau"
replace ccode = 987 if country=="Micronesia (Federated States of)"|country=="Micronesia (FS of)"|country=="Micronesia, Federated States of"
replace ccode = 987 if country=="Micronesia"
replace ccode = 987 if country=="Micronesia, Fed. Sts."
replace ccode = 990 if country=="Samoa"|country=="W. Samoa"|country=="Western Samoa"
replace ccode = 991 if country=="Guam"
replace ccode = 992 if country=="Nauru"
replace ccode = 993 if country=="Niue"
replace ccode = 994 if country=="Cook Islands"
replace ccode = 995 if country=="American Samoa"
replace ccode=35 if country=="Anguilla"
replace ccode=36 if country=="Curaçao"
replace ccode=37 if country=="Montserrat"
replace ccode=38 if country=="Sint Maarten (Dutch part)"
replace ccode=39 if country=="British Virgin Islands"
replace ccode=521 if country=="Somaliland"|country=="Somaliland Region"
replace ccode=353 if country=="Northern Cyprus"|country=="Turk Cyprus"
drop if ccode==.
drop if country=="Swaziland"&year>2018
gen presdpi = .
replace presdpi=1 if system==0
replace presdpi=0 if system==1|system==2
label var presdpi "Presidential (DPI); assembly elec. pres and parl = 0"
gen cyear=ccode*10000+year
sort cyear
save "DPI2020work.dta", replace


********************************************************************************************************************
*Ross oil and gas income data. Downloaded from https://dataverse.harvard.edu/api/access/datafile/2431785. 
*added country ccode index and cyear
********************************************************************************************************************
use "rossshort2014.dta", clear
sort cyear
save "rossshort2014.dta", replace

**************************************************************
*Add Przeworski's PIPE data for presidential system variable
**************************************************************
use "pipepres.dta", clear
label var presidential "pres. system=1, parl. system=0; PIPE; carried forward if data end > 2000"
xtset ccode year
bys ccode: carryforward presidential, replace
sort cyear
save "pipepres.dta", replace

******************************************************************************
*Hanson and Sigman state capacity data
******************************************************************************
use "HansonSigman_source.dta", clear
replace ccode = 212 if country=="Luxembourg"
replace ccode = 255 if country=="FRG/Germany"|country=="West Germany"|country=="Germany, West"|country=="Germany"|country=="West Germany (FRG)"|country=="German Federal Republic"
replace ccode = 31 if country=="Bahamas"
replace ccode = 316 if country=="Czechoslovakia"
replace ccode = 338 if country=="Malta"
replace ccode = 341 if country=="Montenegro"
replace ccode = 342 if country=="Serbia and Montenegro"|country=="Serbia-Montenegro"
replace ccode = 342 if country=="Yugoslavia"
replace ccode = 343 if country=="F.Y.R. Macedonia"|country=="Macedonia, FYR"|country=="FYR Macedonia"|country=="TFYR of Macedonia"|country=="Macedonia"|country=="The former Yugoslav Republic of Macedonia"|country=="Macedonia, the former Yugoslav Republic of"|country=="TFYR Macedonia"|country=="Macedonia (FYROM)"
replace ccode = 347 if country=="Kosovo"
replace ccode = 365 if country=="Russia"|country=="Russian Federation"|country=="Russia/USSR"|country=="USSR"|country=="Soviet Union"
replace ccode = 395 if country=="Iceland"
replace ccode = 53 if country=="Barbados"
replace ccode = 530 if country=="Ethiopia"
replace ccode = 55 if country=="Grenada"
replace ccode = 56 if country=="St. Lucia"
replace ccode = 591 if country=="Seychelles"
replace ccode = 625 if country=="Sudan"|country=="Sudan (Former)"
replace ccode = 626 if country=="South Sudan"
replace ccode = 770 if country=="Pakistan"
replace ccode = 781 if country=="Maldives"
replace ccode = 80 if country=="Belize"
replace ccode = 835 if country=="Brunei"
replace ccode = 935 if country=="Vanuatu"
replace ccode = 990 if country=="Samoa"|country=="W. Samoa"|country=="Western Samoa"
gen cyear=ccode*10000+year
*duplicates
drop if cyear==2551990&Capacity>2
drop if cyear==3422006&Capacity~=.4128
keep cyear Capacity
sort cyear
save "HansonSigmancap.dta", replace

*Boxell et al. 
import delimited "Boxell Gentzkow Shapiro affective polarization data.csv", clear
gen cyear=ccode*10000+year
sort cyear
save "Boxell.dta", replace
 
*Tavits Ward
 use "tavits ward average affect.dta", clear
 collapse (mean) affective_polarization_sd, by( c1004)
 split c1004 , parse(_) generate(newv)
 rename newv1 wbcode
 gen ccode=.
replace ccode= 2 if wbcode=="USA"
replace ccode= 20 if wbcode=="CAN"
replace ccode= 31 if wbcode=="BHM"
replace ccode= 40 if wbcode=="CUB"
replace ccode= 40 if wbcode=="CUB"
replace ccode= 41 if wbcode=="HAI"
replace ccode= 41 if wbcode=="HAI"
replace ccode= 42 if wbcode=="DOM"
replace ccode= 42 if wbcode=="DOM"
replace ccode= 51 if wbcode=="JAM"
replace ccode= 52 if wbcode=="TRI"
replace ccode= 53 if wbcode=="BAR"
replace ccode= 54 if wbcode=="DMA"
replace ccode= 55 if wbcode=="GRN"
replace ccode= 56 if wbcode=="SLU"
replace ccode= 57 if wbcode=="SVG"
replace ccode= 58 if wbcode=="AAB"
replace ccode= 60 if wbcode=="SKN"
replace ccode= 70 if wbcode=="MEX"
replace ccode= 80 if wbcode=="BLZ"
replace ccode= 90 if wbcode=="GUA"
replace ccode= 91 if wbcode=="HON"
replace ccode= 92 if wbcode=="SAL"
replace ccode= 93 if wbcode=="NIC"
replace ccode= 94 if wbcode=="COS"
replace ccode= 95 if wbcode=="PAN"
replace ccode= 100 if wbcode=="COL"
replace ccode= 101 if wbcode=="VEN"
replace ccode= 110 if wbcode=="GUY"
replace ccode= 115 if wbcode=="SUR"
replace ccode= 130 if wbcode=="ECU"
replace ccode= 135 if wbcode=="PER"
replace ccode= 140 if wbcode=="BRA"
replace ccode= 145 if wbcode=="BOL"
replace ccode= 150 if wbcode=="PAR"
replace ccode= 150 if wbcode=="PAR"
replace ccode= 155 if wbcode=="CHL"
replace ccode= 160 if wbcode=="ARG"
replace ccode= 165 if wbcode=="URY"
replace ccode= 200 if wbcode=="UKG"
replace ccode= 205 if wbcode=="IRL"
replace ccode= 210 if wbcode=="NTH"
replace ccode= 210 if wbcode=="NLD"
replace ccode= 211 if wbcode=="BEL"
replace ccode= 211 if wbcode=="BEL"
replace ccode= 212 if wbcode=="LUX"
replace ccode= 212 if wbcode=="LUX"
replace ccode= 220 if wbcode=="FRN"
replace ccode= 220 if wbcode=="FRA"
replace ccode= 221 if wbcode=="MNC"
replace ccode= 223 if wbcode=="LIE"
replace ccode= 225 if wbcode=="CHE"
replace ccode= 230 if wbcode=="ESP"
replace ccode= 232 if wbcode=="AND"
replace ccode= 235 if wbcode=="PRT"
replace ccode= 240 if wbcode=="HAN"
replace ccode= 245 if wbcode=="BAV"
replace ccode= 255 if wbcode=="DEU"
replace ccode= 255 if wbcode=="GMY"
replace ccode= 260 if wbcode=="GFR"
replace ccode= 265 if wbcode=="GDR"
replace ccode= 267 if wbcode=="BAD"
replace ccode= 269 if wbcode=="SAX"
replace ccode= 271 if wbcode=="WRT"
replace ccode= 273 if wbcode=="HSE"
replace ccode= 275 if wbcode=="HSG"
replace ccode= 280 if wbcode=="MEC"
replace ccode= 290 if wbcode=="POL"
replace ccode= 290 if wbcode=="POL"
replace ccode= 300 if wbcode=="AUH"
replace ccode= 305 if wbcode=="AUT"
replace ccode= 900 if wbcode=="AUS"
replace ccode= 310 if wbcode=="HUN"
replace ccode= 315 if wbcode=="CZE"
replace ccode= 315 if wbcode=="CZE"
replace ccode= 316 if wbcode=="CZR"
replace ccode= 317 if wbcode=="SVK"
replace ccode= 325 if wbcode=="ITA"
replace ccode= 327 if wbcode=="PAP"
replace ccode= 329 if wbcode=="SIC"
replace ccode= 331 if wbcode=="SNM"
replace ccode= 332 if wbcode=="MOD"
replace ccode= 335 if wbcode=="PMA"
replace ccode= 337 if wbcode=="TUS"
replace ccode= 338 if wbcode=="MLT"
replace ccode= 339 if wbcode=="ALB"
replace ccode= 339 if wbcode=="ALB"
replace ccode= 341 if wbcode=="MNG"
replace ccode= 343 if wbcode=="MAC"
replace ccode= 344 if wbcode=="HRV"
replace ccode= 345 if wbcode=="YUG"
replace ccode= 345 if wbcode=="YUG"
replace ccode= 346 if wbcode=="BOS"
replace ccode= 347 if wbcode=="KOS"
replace ccode= 349 if wbcode=="SVN"
replace ccode= 350 if wbcode=="GRC"
replace ccode= 350 if wbcode=="GRC"
replace ccode= 352 if wbcode=="CYP"
replace ccode= 355 if wbcode=="BUL"
replace ccode= 359 if wbcode=="MLD"
replace ccode= 360 if wbcode=="ROM"
replace ccode= 365 if wbcode=="RUS"
replace ccode= 366 if wbcode=="EST"
replace ccode= 366 if wbcode=="EST"
replace ccode= 367 if wbcode=="LAT"
replace ccode= 367 if wbcode=="LVA"
replace ccode= 368 if wbcode=="LIT"
replace ccode= 368 if wbcode=="LIT"
replace ccode= 369 if wbcode=="UKR"
replace ccode= 370 if wbcode=="BLR"
replace ccode= 371 if wbcode=="ARM"
replace ccode= 372 if wbcode=="GRG"
replace ccode= 373 if wbcode=="AZE"
replace ccode= 375 if wbcode=="FIN"
replace ccode= 380 if wbcode=="SWE"
replace ccode= 385 if wbcode=="NOR"
replace ccode= 385 if wbcode=="NOR"
replace ccode= 390 if wbcode=="DEN"
replace ccode= 390 if wbcode=="DNK"
replace ccode= 395 if wbcode=="ISL"
replace ccode= 402 if wbcode=="CAP"
replace ccode= 403 if wbcode=="STP"
replace ccode= 404 if wbcode=="GNB"
replace ccode= 411 if wbcode=="EQG"
replace ccode= 420 if wbcode=="GAM"
replace ccode= 432 if wbcode=="MLI"
replace ccode= 433 if wbcode=="SEN"
replace ccode= 434 if wbcode=="BEN"
replace ccode= 435 if wbcode=="MAA"
replace ccode= 436 if wbcode=="NIR"
replace ccode= 437 if wbcode=="CDI"
replace ccode= 438 if wbcode=="GUI"
replace ccode= 439 if wbcode=="BFO"
replace ccode= 450 if wbcode=="LBR"
replace ccode= 451 if wbcode=="SIE"
replace ccode= 452 if wbcode=="GHA"
replace ccode= 461 if wbcode=="TOG"
replace ccode= 471 if wbcode=="CAO"
replace ccode= 475 if wbcode=="NIG"
replace ccode= 481 if wbcode=="GAB"
replace ccode= 482 if wbcode=="CEN"
replace ccode= 483 if wbcode=="CHA"
replace ccode= 484 if wbcode=="CON"
replace ccode= 490 if wbcode=="DRC"
replace ccode= 500 if wbcode=="UGA"
replace ccode= 501 if wbcode=="KEN"
replace ccode= 510 if wbcode=="TAZ"
replace ccode= 511 if wbcode=="ZAN"
replace ccode= 516 if wbcode=="BUI"
replace ccode= 517 if wbcode=="RWA"
replace ccode= 520 if wbcode=="SOM"
replace ccode= 522 if wbcode=="DJI"
replace ccode= 530 if wbcode=="ETH"
replace ccode= 530 if wbcode=="ETH"
replace ccode= 531 if wbcode=="ERI"
replace ccode= 540 if wbcode=="ANG"
replace ccode= 541 if wbcode=="MZM"
replace ccode= 551 if wbcode=="ZAM"
replace ccode= 552 if wbcode=="ZIM"
replace ccode= 553 if wbcode=="MAW"
replace ccode= 560 if wbcode=="ZAF"
replace ccode= 565 if wbcode=="NAM"
replace ccode= 570 if wbcode=="LES"
replace ccode= 571 if wbcode=="BOT"
replace ccode= 572 if wbcode=="SWA"
replace ccode= 580 if wbcode=="MAG"
replace ccode= 581 if wbcode=="COM"
replace ccode= 590 if wbcode=="MAS"
replace ccode= 591 if wbcode=="SEY"
replace ccode= 600 if wbcode=="MOR"
replace ccode= 600 if wbcode=="MOR"
replace ccode= 615 if wbcode=="ALG"
replace ccode= 616 if wbcode=="TUN"
replace ccode= 616 if wbcode=="TUN"
replace ccode= 620 if wbcode=="LIB"
replace ccode= 625 if wbcode=="SUD"
replace ccode= 630 if wbcode=="IRN"
replace ccode= 640 if wbcode=="TUR"
replace ccode= 645 if wbcode=="IRQ"
replace ccode= 651 if wbcode=="EGY"
replace ccode= 651 if wbcode=="EGY"
replace ccode= 652 if wbcode=="SYR"
replace ccode= 652 if wbcode=="SYR"
replace ccode= 660 if wbcode=="LEB"
replace ccode= 663 if wbcode=="JOR"
replace ccode= 666 if wbcode=="ISR"
replace ccode= 670 if wbcode=="SAU"
replace ccode= 678 if wbcode=="YAR"
replace ccode= 679 if wbcode=="YEM"
replace ccode= 680 if wbcode=="YPR"
replace ccode= 690 if wbcode=="KUW"
replace ccode= 692 if wbcode=="BAH"
replace ccode= 694 if wbcode=="QAT"
replace ccode= 696 if wbcode=="UAE"
replace ccode= 698 if wbcode=="OMA"
replace ccode= 700 if wbcode=="AFG"
replace ccode= 701 if wbcode=="TKM"
replace ccode= 702 if wbcode=="TAJ"
replace ccode= 703 if wbcode=="KYR"
replace ccode= 704 if wbcode=="UZB"
replace ccode= 705 if wbcode=="KZK"
replace ccode= 710 if wbcode=="CHN"
replace ccode= 712 if wbcode=="MON"
replace ccode= 713 if wbcode=="TAW"
replace ccode= 730 if wbcode=="KOR"
replace ccode= 731 if wbcode=="PRK"
replace ccode= 732 if wbcode=="ROK"
replace ccode= 740 if wbcode=="JPN"
replace ccode= 740 if wbcode=="JPN"
replace ccode= 750 if wbcode=="IND"
replace ccode= 760 if wbcode=="BHU"
replace ccode= 770 if wbcode=="PAK"
replace ccode= 771 if wbcode=="BNG"
replace ccode= 775 if wbcode=="MYA"
replace ccode= 780 if wbcode=="SRI"
replace ccode= 781 if wbcode=="MAD"
replace ccode= 790 if wbcode=="NEP"
replace ccode= 800 if wbcode=="THI"
replace ccode= 811 if wbcode=="CAM"
replace ccode= 812 if wbcode=="LAO"
replace ccode= 816 if wbcode=="DRV"
replace ccode= 817 if wbcode=="RVN"
replace ccode= 820 if wbcode=="MAL"
replace ccode= 830 if wbcode=="SIN"
replace ccode= 835 if wbcode=="BRU"
replace ccode= 840 if wbcode=="PHL"
replace ccode= 850 if wbcode=="INS"
replace ccode= 860 if wbcode=="ETM"
replace ccode= 900 if wbcode=="AUL"
replace ccode= 910 if wbcode=="PNG"
replace ccode= 920 if wbcode=="NZL"
replace ccode= 935 if wbcode=="VAN"
replace ccode= 940 if wbcode=="SOL"
replace ccode= 946 if wbcode=="KIR"
replace ccode= 947 if wbcode=="TUV"
replace ccode= 950 if wbcode=="FIJ"
replace ccode= 955 if wbcode=="TON"
replace ccode= 970 if wbcode=="NAU"
replace ccode= 983 if wbcode=="MSI"
replace ccode= 986 if wbcode=="PAL"
replace ccode= 987 if wbcode=="FSM"
replace ccode= 990 if wbcode=="WSM"
replace ccode= 713 if wbcode=="TWN"
destring newv2, gen(year)

gen cyear=ccode*10000+year
sort cyear
save "tavits ward work.dta", replace

*********************************************************************************
*MAINWARING AND PEREZ-LINAN
*********************************************************************************
*use "https://kellogg.nd.edu/sites/default/files/ddla.dta", clear
import excel "DDLA.XLSX", sheet("Data") firstrow clear
gen ccode=.
replace ccode = 100 if country=="Colombia"
replace ccode = 1001 if country=="Hong Kong"|country=="China: Hong Kong SAR"|country=="Hong Kong SAR"|country=="Hong Kong SAR, China"|country=="China, Hong Kong SAR"|country=="Hong Kong, China"
replace ccode = 1002 if country=="Puerto Rico"
replace ccode = 1005 if country=="Palestine"|country=="State of Palestine"|country=="Palestinian Territories"|country=="West Bank and Gaza Strip"|country=="West Bank and Gaza"|country=="Israeli-Occupied Territories and Palestinian Authority"|country=="Palestinian Authority"|country=="West Bank"|country=="Palestine, State of"
replace ccode = 101 if country=="Venezuela"|country=="Venezuela (Bolivarian Republic of)"|country=="Venezuela, RB"|country=="Venezuela, Bolivarian Republic of"
replace ccode = 110 if country=="Guyana"
replace ccode = 115 if country=="Suriname"
replace ccode = 130 if country=="Ecuador"
replace ccode = 135 if country=="Peru"
replace ccode = 140 if country=="Brazil"
replace ccode = 145 if country=="Bolivia"|country=="Bolivia (Plurinational State of)"|country=="Bolivia, Plurinational State of"
replace ccode = 150 if country=="Paraguay"
replace ccode = 155 if country=="Chile"
replace ccode = 160 if country=="Argentina"
replace ccode = 165 if country=="Uruguay"
replace ccode = 166 if country=="Faeroe Islands"
replace ccode = 2 if country=="United States of America"|country=="United States"|country=="USA"|country=="US"
replace ccode = 20 if country=="Canada"
replace ccode = 200 if country=="Great Britain"|country=="United Kingdom"|country=="UK"
replace ccode = 201 if country=="Channel Islands"
replace ccode = 202 if country=="Isle of Man"
replace ccode = 205 if country=="Ireland"
replace ccode = 210 if country=="Netherlands"
replace ccode = 211 if country=="Belgium"
replace ccode = 212 if country=="Luxembourg"
replace ccode = 220 if country=="France"
replace ccode = 221 if country=="Monaco"
replace ccode = 223 if country=="Liechtenstein"
replace ccode = 225 if country=="Switzerland"
replace ccode = 230 if country=="Spain"
replace ccode = 232 if country=="Andorra"
replace ccode = 235 if country=="Portugal"
replace ccode = 255 if country=="FRG/Germany"|country=="West Germany"|country=="Germany, West"|country=="Germany"|country=="West Germany (FRG)"|country=="German Federal Republic"
replace ccode = 265 if country=="GDR"|country=="East Germany"|country=="Germany, East"|country=="East Germany (GDR)"|country=="Germany East"|country=="German Democratic Republic"
replace ccode = 290 if country=="Poland"
replace ccode = 305 if country=="Austria"
replace ccode = 31 if country=="Bahamas"
replace ccode = 31 if country=="Bahamas"|country=="Bahamas, The"|country=="The Bahamas"
replace ccode = 310 if country=="Hungary"
replace ccode = 316 if country=="Czech Rep."|country=="Czech Republic"|country=="Czechia"
replace ccode = 316 if country=="Czechoslovakia"
replace ccode = 317 if country=="Slovak Republic"|country=="Slovakia"|country=="Slovak Rep."
replace ccode = 32 if country=="Aruba"
replace ccode = 325 if country=="Italy"
replace ccode = 33 if country=="Bermuda"
replace ccode = 331 if country=="San Marino"
replace ccode = 338 if country=="Malta"
replace ccode = 339 if country=="Albania"
replace ccode = 341 if country=="Montenegro"
replace ccode = 342 if country=="Serbia and Montenegro"|country=="Serbia-Montenegro"
replace ccode = 342 if country=="Serbia"
replace ccode = 342 if country=="Yugoslavia"
replace ccode = 343 if country=="F.Y.R. Macedonia"|country=="Macedonia, FYR"|country=="FYR Macedonia"|country=="TFYR of Macedonia"|country=="Macedonia"|country=="The former Yugoslav Republic of Macedonia"|country=="Macedonia, the former Yugoslav Republic of"|country=="TFYR Macedonia"|country=="Macedonia (FYROM)"
replace ccode = 344 if country=="Croatia"
replace ccode = 346 if country=="Bosnia and Herzegovina"|country=="Bosnia"|country=="Bosnia-Herz"|country=="Bosnia-Herzegovina"|country=="Bosnia & Herzegovina"
replace ccode = 347 if country=="Kosovo"
replace ccode = 349 if country=="Slovenia"
replace ccode = 350 if country=="Greece"
replace ccode = 352 if country=="Cyprus"|country=="Cyprus (Greek)"
replace ccode = 355 if country=="Bulgaria"
replace ccode = 359 if country=="Moldova"|country=="Republic of Moldova"|country=="Moldova, Republic of"
replace ccode = 360 if country=="Romania"
replace ccode = 365 if country=="Russia"|country=="Russian Federation"|country=="Russia/USSR"|country=="USSR"|country=="Soviet Union"
replace ccode = 366 if country=="Estonia"
replace ccode = 367 if country=="Latvia"
replace ccode = 368 if country=="Lithuania"
replace ccode = 369 if country=="Ukraine"
replace ccode = 370 if country=="Belarus"
replace ccode = 371 if country=="Armenia"
replace ccode = 372 if country=="Georgia"
replace ccode = 373 if country=="Azerbaijan"
replace ccode = 375 if country=="Finland"
replace ccode = 380 if country=="Sweden"
replace ccode = 385 if country=="Norway"
replace ccode = 390 if country=="Denmark"
replace ccode = 395 if country=="Iceland"
replace ccode = 396 if country=="Greenland"
replace ccode = 40 if country=="Cuba"
replace ccode = 402 if country=="C. Verde Is."|country=="Cabo Verde"|country=="Cape Verde"
replace ccode = 403 if country=="Sao Tome and Principe"|country=="São Tomé and Príncipe"|country=="Sao Tome"|country=="S. Tomé & Principe"
replace ccode = 404 if country=="Guinea-Bissau"|country=="Guinea Bissau"
replace ccode = 41 if country=="Haiti"
replace ccode = 411 if country=="Eq. Guinea"|country=="Equatorial Guinea"
replace ccode = 42 if country=="Dom. Rep."|country=="Dominican Rep."|country=="Dominican Republic"|country=="Dominican Rep"
replace ccode = 420 if country=="Gambia"|country=="Gambia, The"|country=="The Gambia"
replace ccode = 432 if country=="Mali"
replace ccode = 433 if country=="Senegal"
replace ccode = 434 if country=="Benin"
replace ccode = 435 if country=="Mauritania"
replace ccode = 436 if country=="Niger"
replace ccode = 437 if country=="C?te d'Ivoire"|country=="Cote d`Ivoire"|country=="Côte d´Ivoire"|country=="Côte d’Ivoire"|country=="Côte d'Ivoire"|country=="Cote d'Ivoire"|country=="Ivory Coast (Cote d'Ivoire)"|country=="Ivory Coast"|country=="Côte d'Ivoire"
replace ccode = 438 if country=="Guinea"
replace ccode = 439 if country=="Burkina Faso"
replace ccode = 450 if country=="Liberia"
replace ccode = 451 if country=="Sierra Leone"
replace ccode = 452 if country=="Ghana"
replace ccode = 461 if country=="Togo"
replace ccode = 471 if country=="Cameroon"
replace ccode = 475 if country=="Nigeria"
replace ccode = 481 if country=="Gabon"
replace ccode = 482 if country=="Cent. Af. Rep."
replace ccode = 482 if country=="Central Afr. Rep."|country=="Cen African Rep"|country=="Central African Rep."
replace ccode = 482 if country=="Central African Republic"
replace ccode = 483 if country=="Chad"
replace ccode = 484 if country=="Congo (Brazzaville)"
replace ccode = 484 if country=="Congo Brazzaville"|country=="Congo-Brz"
replace ccode = 484 if country=="Congo Republic"
replace ccode = 484 if country=="Congo"
replace ccode = 484 if country=="Congo, Rep. Of"
replace ccode = 484 if country=="Congo, Rep."|country=="Congo-Brazzaville"|country=="Republic of Congo"|country=="Congo, Republic"|country=="Republic of the Congo"
replace ccode = 484 if country=="Congo, Republic of"|country=="Congo, Republic of (Brazzaville)"
replace ccode = 484 if country=="Congo, Republicof"|country=="Congo (Rep.)"
replace ccode = 490 if country=="Congo (Democratic Republic of the)"|country=="Congo, the Democratic Republic of the"|country=="Congo (Dem. Rep.)"
replace ccode = 490 if country=="Congo (DRC)"|country=="Congo, Democratic Republic of (Kinshasa)"|country=="Zaire (Democ Republic Congo)"|country=="Congo/Zaire"
replace ccode = 490 if country=="Congo (Kinshasa)"|country=="Congo-Kinshasa"
replace ccode = 490 if country=="Congo, Dem. R."
replace ccode = 490 if country=="Congo, Dem. Rep."|country=="Congo, Democratic Republic"|country=="Democratic Republic of Congo"|country=="Zaire"
replace ccode = 490 if country=="Congo, Democratic Republic of"|country=="Congo, Dem. Rep."|country=="D.R. of the Congo"
replace ccode = 490 if country=="Democratic Republic of the Congo"
replace ccode = 500 if country=="Uganda"
replace ccode = 501 if country=="Kenya"
replace ccode = 51 if country=="Jamaica"
replace ccode = 510 if country=="Tanzania (United Republic of)"|country=="U.R. of Tanzania: Mainland"|country=="Tanzania, United Republic of"
replace ccode = 510 if country=="Tanzania"|country=="United Republic of Tanzania"
replace ccode = 516 if country=="Burundi"
replace ccode = 517 if country=="Rwanda"
replace ccode = 52 if country=="Trinidad & Tob."
replace ccode = 52 if country=="Trinidad & Tobago"
replace ccode = 52 if country=="Trinidad and Tobago"
replace ccode = 52 if country=="Trinidad-Tobago"
replace ccode = 520 if country=="Somalia"
replace ccode = 522 if country=="Djibouti"
replace ccode = 53 if country=="Barbados"
replace ccode = 530 if country=="Ethiopia"
replace ccode = 531 if country=="Eritrea"
replace ccode = 54 if country=="Dominica"
replace ccode = 540 if country=="Angola"
replace ccode = 541 if country=="Mozambique"
replace ccode = 55 if country=="Grenada"
replace ccode = 551 if country=="Zambia"
replace ccode = 552 if country=="Zimbabwe"|country=="Rhodesia"
replace ccode = 553 if country=="Malawi"
replace ccode = 56 if country=="Saint Lucia"
replace ccode = 56 if country=="St. Lucia"
replace ccode = 560 if country=="S. Africa"
replace ccode = 560 if country=="South Africa"
replace ccode = 565 if country=="Namibia"
replace ccode = 57 if country=="Saint Vincent and the Grenadines"|country=="St Vincent"
replace ccode = 57 if country=="St. Vincent & Grenadines"
replace ccode = 57 if country=="St. Vincent and Grenadines"
replace ccode = 57 if country=="St. Vincent and the Grenadines"
replace ccode = 570 if country=="Lesotho"
replace ccode = 571 if country=="Botswana"
replace ccode = 572 if country=="Swaziland"
replace ccode = 58 if country=="Antigua and Barbuda"|country=="Antigua & Barbuda"
replace ccode = 580 if country=="Madagascar"
replace ccode = 581 if country=="Comoro Is."
replace ccode = 581 if country=="Comoros"
replace ccode = 59 if country=="Cayman Islands"
replace ccode = 590 if country=="Mauritius"
replace ccode = 591 if country=="Seychelles"
replace ccode = 60 if country=="Saint Kitts & Nevis"|country=="Saint Kitts and Nevis"|country=="St Kitts"
replace ccode = 60 if country=="St. Kitts and Nevis"
replace ccode = 60 if country=="St.Kitts & Nevis"
replace ccode = 600 if country=="Morocco"
replace ccode = 61 if country=="Curacao"
replace ccode = 615 if country=="Algeria"
replace ccode = 616 if country=="Tunisia"
replace ccode = 62 if country=="Turks and Caicos Islands"
replace ccode = 620 if country=="Libya"
replace ccode = 625 if country=="Sudan"|country=="Sudan (Former)"
replace ccode = 626 if country=="South Sudan"
replace ccode = 63 if country=="Virgin Islands (U.S.)"
replace ccode = 630 if country=="Iran (Islamic Republic of)"|country=="Iran, Islamic Republic of"
replace ccode = 630 if country=="Iran"|country=="Iran (I.R.)"
replace ccode = 630 if country=="Iran"
replace ccode = 630 if country=="Iran, Islamic Rep."|country=="Islamic Republic of Iran"
replace ccode = 640 if country=="Turkey"
replace ccode = 645 if country=="Iraq"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt, Arab Rep."
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syrian Arab Republic"
replace ccode = 660 if country=="Lebanon"
replace ccode = 663 if country=="Jordan"
replace ccode = 666 if country=="Israel"|country=="Israel and Occupied Territories**"|country=="Israel in pre-1967 borders"
replace ccode = 670 if country=="Saudi Arabia"
replace ccode = 678 if country=="Yemen Arab Republic"|country=="Yemen (A.R.)"|country=="North Yemen"|country=="Yemen (AR)"
replace ccode = 679 if country=="Yemen, Rep."|country=="Yemen"|country=="Yemen, Republic of"
replace ccode = 680 if country=="Yemen (PDR)"|country=="South Yemen"|country=="Yemen, South"|country=="SouthYemen"|country=="Yemen People's Republic"|country=="Yemen  (PDR)"|country=="Yemen South"
replace ccode = 690 if country=="Kuwait"
replace ccode = 692 if country=="Bahrain"
replace ccode = 694 if country=="Qatar"
replace ccode = 696 if country=="Unit. Arab Em."
replace ccode = 696 if country=="United Arab Emirates"|country=="UAE"|country=="United Arab Emirates#"
replace ccode = 698 if country=="Oman"
replace ccode = 70 if country=="Mexico"
replace ccode = 700 if country=="Afghanistan"
replace ccode = 701 if country=="Turkmenistan"
replace ccode = 702 if country=="Tajikistan"
replace ccode = 703 if country=="Kyrgyz Republic"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 704 if country=="Uzbekistan"
replace ccode = 705 if country=="Kazakhstan"
replace ccode = 710 if country=="China (PRC)"
replace ccode = 710 if country=="China"
replace ccode = 710 if country=="China, P. R."
replace ccode = 710 if country=="PRC"
replace ccode = 711 if country=="China: Macao SAR"|country=="Macao, China"
replace ccode = 711 if country=="Macao SAR, China"|country=="China, Macao SAR"
replace ccode = 711 if country=="Macao"|country=="Macau"
replace ccode = 712 if country=="Mongolia"
replace ccode = 713 if country=="Taiwan (ROC)"|country=="Taiwan Province of China"|country=="Taiwan, China"|country=="Taiwan, Province of China"
replace ccode = 713 if country=="Taiwan"
replace ccode = 713 if country=="Taiwan"
replace ccode = 731 if country=="Korea (North)"|country=="Korea North"|country=="Korea, Democratic People's Republic of"
replace ccode = 731 if country=="Korea, Dem. Rep."|country=="Democratic People's Republic of Korea"|country=="D.P.R. Korea"
replace ccode = 731 if country=="North Korea"|country=="Korea, North"
replace ccode = 731 if country=="PRK"|country=="North Korea (Democratc People's Republic of Korea)"
replace ccode = 732 if country=="Korea (South)"|country=="Korea South"
replace ccode = 732 if country=="Korea, Rep."|country=="Korea"|country=="Korea (Rep.)"
replace ccode = 732 if country=="Korea, Republic of"
replace ccode = 732 if country=="Korea, South"
replace ccode = 732 if country=="ROK"
replace ccode = 732 if country=="South Korea"|country=="Korea, South"|country=="Republic of Korea"|country=="South Korea (Republic of Korea)"
replace ccode = 740 if country=="Japan"
replace ccode = 750 if country=="India"
replace ccode = 760 if country=="Bhutan"
replace ccode = 770 if country=="Pakistan"
replace ccode = 771 if country=="Bangladesh"
replace ccode = 775 if country=="Burma (Myanmar)"
replace ccode = 775 if country=="Burma"
replace ccode = 775 if country=="Myanmar"|country=="Myanmar (Burma)"
replace ccode = 780 if country=="Sri Lanka"
replace ccode = 781 if country=="Maldives"
replace ccode = 790 if country=="Nepal"
replace ccode = 80 if country=="Belize"
replace ccode = 800 if country=="Thailand"
replace ccode = 811 if country=="Cambodia"
replace ccode = 812 if country=="Lao PDR"|country=="Lao P.D.R."
replace ccode = 812 if country=="Lao People's Democratic Republic"
replace ccode = 812 if country=="Lao"|country=="Lao People's DR"
replace ccode = 812 if country=="Laos"
replace ccode = 812 if country=="Laos"
replace ccode = 817 if country=="Vietnam, South"|country=="South Vietnam"|country=="Vietnam South"
replace ccode = 818 if country=="Viet Nam"|country=="Vietnam, Socialist Republic of"
replace ccode = 818 if country=="Vietnam"
replace ccode = 820 if country=="Malaysia"
replace ccode = 830 if country=="Singapore"
replace ccode = 835 if country=="Brunei Darussalam"
replace ccode = 835 if country=="Brunei"
replace ccode = 835 if country=="Brunei"
replace ccode = 840 if country=="Philippines"|country=="Philippines +"
replace ccode = 850 if country=="Indonesia"
replace ccode = 860 if country=="East Timor"|country=="East Timor (Timor L'este)"
replace ccode = 860 if country=="Timor-Leste"|country=="Timor Leste"
replace ccode = 90 if country=="Guatemala"
replace ccode = 900 if country=="Australia"
replace ccode = 91 if country=="Honduras"
replace ccode = 910 if country=="P.N. Guinea"
replace ccode = 910 if country=="Papua New Guinea"|country=="P.N. Guinea"|country=="P. N. Guinea"
replace ccode = 92 if country=="El Salvador"
replace ccode = 920 if country=="New Zealand"
replace ccode = 93 if country=="Nicaragua"
replace ccode = 935 if country=="Vanuatu"
replace ccode = 94 if country=="Costa Rica"
replace ccode = 940 if country=="Solomon Is."
replace ccode = 940 if country=="Solomon Islands"
replace ccode = 941 if country=="Northern Mariana Islands"
replace ccode = 946 if country=="Kiribati"
replace ccode = 947 if country=="Tuvalu"
replace ccode = 95 if country=="Panama"
replace ccode = 950 if country=="Fiji"
replace ccode = 951 if country=="French Polynesia"
replace ccode = 952 if country=="New Caledonia"
replace ccode = 955 if country=="Tonga"
replace ccode = 983 if country=="Marshall Islands"
replace ccode = 986 if country=="Palau"
replace ccode = 987 if country=="Micronesia (Federated States of)"|country=="Micronesia (FS of)"|country=="Micronesia, Federated States of"
replace ccode = 987 if country=="Micronesia"
replace ccode = 987 if country=="Micronesia, Fed. Sts."
replace ccode = 990 if country=="Samoa"|country=="W. Samoa"|country=="Western Samoa"
replace ccode = 991 if country=="Guam"
replace ccode = 992 if country=="Nauru"
replace ccode = 993 if country=="Niue"
replace ccode = 994 if country=="Cook Islands"
replace ccode = 995 if country=="American Samoa"
replace ccode=35 if country=="Anguilla"
replace ccode=36 if country=="Curaçao"
replace ccode=37 if country=="Montserrat"
replace ccode=38 if country=="Sint Maarten (Dutch part)"
replace ccode=39 if country=="British Virgin Islands"
replace ccode=521 if country=="Somaliland"|country=="Somaliland Region"
replace ccode=353 if country=="Northern Cyprus"
gen cyear=ccode*10000+year
sort cyear
save "linanwork.dta", replace






********/
*MERGE*/
********/

use "breakdown cases.dta", clear
sort cyear 
merge 1:1 cyear using "mad2020wk.dta"
drop _merge
sort cyear
merge 1:1 cyear using "lied_v6wk.dta"
drop _merge
sort cyear
merge 1:1 cyear using "vdem12wk.dta"
drop _merge
sort cyear
merge 1:1 cyear using "llautocratization.dta"
drop _merge
sort cyear
merge cyear using "fh20pollong.dta"
drop _merge
sort cyear
merge cyear using "fh20civlong.dta"
drop _merge
sort cyear
merge cyear using "FHPRCL.dta"
drop _merge
sort cyear
merge 1:1 cyear using "fh20statlong.dta"
drop _merge
sort cyear
merge 1:1 cyear using "rossshort2014.dta"
drop _merge
sort cyear
merge 1:1 cyear using "swiid9_0work.dta"
drop _merge
sort cyear
merge 1:1 cyear using "pipepres.dta"
drop _merge
bys ccode: carryforward presidential, replace
sort cyear
merge cyear using "polnei.dta"
drop _merge
sort cyear
merge cyear using "vdemnei.dta"
drop _merge
sort cyear
merge cyear using "vdemneil.dta"
drop _merge
sort cyear
merge cyear using "fhfnei.dta"
drop _merge
drop if ccode>1008|ccode==.
sort cyear
merge cyear using "claassen.dta"
drop _merge
sort cyear
merge cyear using "bkdown cases.dta"
drop _merge
sort cyear
merge cyear using "DPI2020work.dta"
drop _merge
sort cyear
merge cyear using "liednei.dta"
drop _merge
drop if ccode==.|cyear==.
sort cyear
merge cyear using "HansonSigmancap.dta"
drop _merge 
sort cyear
merge cyear using "madgdppc.dta"
drop _merge 
sort cyear
merge cyear using "Boxell.dta"
drop _merge
sort cyear
merge cyear using "tavits ward work.dta"
drop _merge
sort cyear
save "breakdown cases.dta", replace



*******************************************************************************
*CONSTRUCTING VARIABLES
*******************************************************************************
gen prespipedpi = .
replace prespipedpi=1 if (presidential==1&year<2001)|(presdpi==1&year>2000)
replace prespipedpi=0 if (presidential==0&year<2001)|(presdpi==0&year>2000)
*Montenegro
replace prespipedpi=0 if ccode==341
*Switzerland somehow got dropped.
replace prespipedpi=0 if ccode==225
bys ccode: carryforward prespipedpi, replace

*Use the Fariss data estimating gdp based on Maddison 2020 data
xtset ccode year
gen lngdppcf = ln(madgdppcf)
gen llngdppcf = l.lngdppcf
gen madgrowthf = ((madgdppcf/l.madgdppcf)-1)*100

xtset ccode year
replace fhpol=l.fhpol if year==1982&fhpol==.
replace fhciv=l.fhciv if year==1982&fhciv==.
replace fhstat=fhstat[_n-1] if year==1982&fhstat==""

gen fhpolfree = 0
replace fhpolfree=1 if fhstat=="F"
replace fhpolfree=. if fhstat==""
label var fhpolfree "FH says country 'free'"
xtset ccode year
gen lfhpolfree=l.fhpolfree

gen fhfreepf = 0
replace fhfreepf=1 if fhstat=="F"|fhstat=="PF"
replace fhfreepf=. if fhstat==""
label var fhfreepf "FH says country 'free' or 'partly free'"

gen fhpol1 = 0
replace fhpol1=1 if fhpol==1
replace fhpol1=. if fhpol==.
label var fhpol1 "FH political rights highest score, 1"

gen vdemel = 0
replace vdemel=1 if v2x_regime==2
replace vdemel=. if v2x_regime==.
label var vdemel "VDEM electoral democracy: v2x_regime=2"
xtset ccode year
gen lvdemel = l.vdemel

bys ccode: carryforward v2elmulpar_osp, replace
bys ccode: carryforward v2elfrfair_osp, replace

gen vdemelo=0
replace vdemelo=1 if v2elmulpar_osp>2&v2elmulpar_osp~=.&v2elfrfair_osp>2&v2elfrfair_osp~=.&v2x_polyarchy>0.5&v2x_polyarchy~=.
replace vdemelo=. if v2elmulpar_osp==.|v2elfrfair_osp==.|v2x_polyarchy==.

gen vdemlibo=0
replace vdemlibo=1 if v2elmulpar_osp>2&v2elmulpar_osp~=.&v2elfrfair_osp>2&v2elfrfair_osp~=.&v2x_polyarchy>0.5&v2x_polyarchy~=.&v2x_liberal>0.8&v2x_liberal~=.
replace vdemlibo=. if v2elmulpar_osp==.|v2elfrfair_osp==.|v2x_polyarchy==.|v2x_liberal==.
label var vdemlibo "VDEM liberal democracy old"

xtset ccode year
replace autocratization=0 if autocratization==.&v2x_polyarchy~=.
gen autfirst = 0
replace autfirst=1 if autocratization==1&l.autocratization==0
replace autfirst=. if autocratization==.
label var autfirst "first yr of LL2019 autocratization"

drop if cyear==.
replace polbkd=. if year>2018
*more accurate dating of Fiji change
replace polbkd=1 if country=="Fiji"&year==2000
replace polbkd=0 if country=="Fiji"&year==2001
*replacing missing data for countries invaded by Germany 1940
replace polbkd=1 if country=="France"&year==1940
replace polbkd=1 if country=="Netherlands"&year==1940
replace polbkd=1 if country=="Belgium"&year==1940
replace polbkd=1 if country=="Luxembourg"&year==1940
replace polbkd=1 if country=="Norway"&year==1940
replace polbkd=1 if country=="Denmark"&year==1940

gen fhstatn=.
replace fhstatn=1 if fhstat=="F"
replace fhstatn=2 if fhstat=="PF"
replace fhstatn=3 if fhstat=="NF"
label var fhstatn "FN status 1=free, 2=partly free, 3=not free"

gen fhpolrn = (7-fhpol)/6
*normalized to 0-1 and reversed

xtset ccode year
*cases in which vdemel falls
xtset ccode year
gen vdemelbkd=.
replace vdemelbkd=1 if l.vdemel==1&v2x_regime<2
replace vdemelbkd=0 if l.vdemel==1&v2x_regime>1&v2x_regime<4
label var vdemelbkd "1 if was vdemel and becomes aut; 0 if was vdemel and does not become aut"

*cases in which vdemlib falls
gen vdemlibbkd=.
replace vdemlibbkd=0 if l.vdemlib==1&vdemlib==1
replace vdemlibbkd=1 if l.vdemlib==1&v2x_regime<3
label var vdemlibbkd "1 if was vdemlib and becomes aut or vdemel; 0 if was and stays vdemlib"

*cases in which vdemlib falls a stat sig distance belows the threshold
gen vdemlibbkdsig=.
replace vdemlibbkdsig=1 if l.v2x_regime==3&v2x_regime_amb<7
replace vdemlibbkdsig=0 if l.v2x_regime==3&v2x_regime_amb>=7
replace vdemlibbkdsig=. if l.v2x_regime==.|v2x_regime_amb==.

*cases in which vdemlib falls using old vdemlib
gen vdemlibbkdo=.
replace vdemlibbkdo=0 if l.vdemlibo==1&vdemlibo==1
replace vdemlibbkdo=1 if l.vdemlibo==1&vdemlibo==0
label var vdemlibbkdo "1 if was vdemlib and becomes aut or vdemel; 0 if was and stays vdemlib; old"

*cases in which vdem democracy becomes autocracy
gen vdemdembkd=.
replace vdemdembkd=1 if l.v2x_regime>1&l.v2x_regime<4&v2x_regime<2
replace vdemdembkd=0 if l.v2x_regime>1&l.v2x_regime<4&v2x_regime>1&v2x_regime<4
label var vdemdembkd "1 if was vdemlib or vdemel and becomes aut; 0 if was and stays one of vdemel and vdemlib"
*Montenegro should not be vdemdembkd=1 in 2006 since only became independent that year. 
*replace vdemdembkd=. if ccode==341&year==2006


*cases in which vdem with old vdemlib democracy becomes autocracy
xtset ccode year
gen vdemdembkdo=.
replace vdemdembkdo=1 if l.v2x_regime>1&l.v2x_regime<4&v2x_regime<2
replace vdemdembkdo=0 if l.v2x_regime>1&l.v2x_regime<4&v2x_regime>1&v2x_regime<4
label var vdemdembkdo"1 if was vdemlib or vdemel and becomes aut; 0 if was and stays one of vdemel and vdemlib; old"

*cases in which fhpolfree from 1 to 0
gen fhpolbkd=0
replace fhpolbkd=1 if l.fhpolfree==1&fhpolfree==0
replace fhpolbkd=. if fhpolfree==.
label var fhpolbkd "was FH 'free', becomes FH not 'free'"
*combine this with fhfbkd
replace fhfbkd=1 if fhpolbkd==1
replace fhpolbkd=1 if fhfbkd==1

*cases in which fhpol1 from 1 to 0
gen fhpol1bkd=.
replace fhpol1bkd=1 if l.fhpol1==1&fhpol1==0
replace fhpol1bkd=0 if l.fhpol1==1&fhpol1==1
label var fhpol1bkd "1 if goes from fhpol=1 to fhpol>1; 0 if was and stays fhpol=1"

xtset ccode year
gen fhfpolwrse = .
replace fhfpolwrse=1 if l.fhstatn==1&l.fhpol<fhpol&fhpol~=.
replace fhfpolwrse=0 if l.fhstatn==1&l.fhpol>=fhpol&l.fhpol~=.
bys year: egen mfhfpolwrse=mean(fhfpolwrse)
label var mfhfpolwrse "proportion of FH 'free' states that worsen on pol rights"
*remember that Freedom House scores are counterintuitively scored with 1 = most free, 7 least free

xtset ccode year
gen fhfcivwrse = .
replace fhfcivwrse=1 if l.fhstatn==1&fhciv>l.fhciv&fhciv~=.
replace fhfcivwrse=0 if l.fhstatn==1&fhciv<=l.fhciv&l.fhciv~=.
bys year: egen mfhfcivwrse=mean(fhfcivwrse)
label var mfhfcivwrse "proportion of FH 'free' states that worsen on civ libs"
*remember that Freedom House scores are counterintuitively scored with 1 = most free, 7 least free

xtset ccode year
gen politydwrse = .
replace politydwrse=1 if l.polity2>=6&l.polity2~=.&polity2<l.polity2
replace politydwrse=0 if l.polity2>=6&l.polity2~=.&polity2>=l.polity2&polity2~=.
bys year: egen mpolitydwrse=mean(politydwrse)
label var mpolitydwrse "proportion of pol2 dems that go down"

xtset ccode year
gen vdemlibwrse = .
replace vdemlibwrse=1 if l.vdemlib==1&v2x_libdem<l.v2x_libdem&l.v2x_libdem~=.
replace vdemlibwrse=0 if l.vdemlib==1&v2x_libdem>=l.v2x_libdem&v2x_libdem~=.
bys year: egen zz=mean(vdemlibwrse)
bys year: egen mvdemlibwrse = max(zz)
drop zz
label var mvdemlibwrse "proportion of vdem lib dems that deteriorate"
xtset ccode year
gen vdemldiff = v2x_libdem-l.v2x_libdem if vdemlibwrse==1
bys year: egen zz=mean(vdemldiff)
bys year: egen mvdemldiff = max(zz)
drop zz

xtset ccode year
gen vdemelwrse = .
replace vdemelwrse=1 if l.vdemel==1&v2x_polyarchy<l.v2x_polyarchy
replace vdemelwrse=0 if l.vdemel==1&v2x_polyarchy>=l.v2x_polyarchy
bys year: egen zz=mean(vdemelwrse)
bys year: egen mvdemelwrse = max(zz)
drop zz
label var mvdemelwrse "proportion of vdem elec dems that deteriorate"

xtset ccode year
gen vdemdemwrse = .
replace vdemdemwrse=1 if l.vdemdem==1&v2x_polyarchy<l.v2x_polyarchy&l.v2x_polyarchy~=.
replace vdemdemwrse=0 if l.vdemdem==1&v2x_polyarchy>=l.v2x_polyarchy&v2x_polyarchy~=.
bys year: egen zz=mean(vdemdemwrse)
bys year: egen mvdemdemwrse = max(zz)
drop zz
label var mvdemdemwrse "proportion of vdem dems that deteriorate"
xtset ccode year
gen lmvdemdemwrse=l.mvdemdemwrse

xtset ccode year
gen liedwrse = .
replace liedwrse=1 if l.lied6==1&lied6==0
replace liedwrse=0 if l.lied6==1&lied6==1
bys year: egen zz=mean(liedwrse)
bys year: egen mliedwrse = max(zz)
drop zz
label var mliedwrse "proportion of lied dems that break down"
xtset ccode year
gen lmliedwrse = l.mliedwrse

bys year: egen mpol =mean(polity2)
bys year: egen mpol2norm =mean(pol2norm)
replace mpol2norm=. if year>2018
bys year: egen mfhpol =mean(fhpol)
bys year: egen mfhpolrn = mean(fhpolrn)
bys year: egen mfhciv=mean(fhciv)
bys year: egen mvdempoly= mean(v2x_polyarchy)
bys year: egen mvdemld= mean(v2x_libdem)

bys year: egen mpol2more5 = mean(pol2more5)
replace mpol2more5=. if year>2018
*Polity somewhat bizarrely has coded the US--but no other countries--for 2019 and 2020. And 
*it codes the US as falling from Polity2 of 7 in 2019 to Polity2 of 5 in 2020.
*That is, the Polity team thinks that US democracy in 2020 was on the same level
*as that of Somalia in 2018. 
bys year: egen mfhpolfree = mean(fhpolfree)
bys year: egen mfhpol1 = mean(fhpol1)
bys year: egen mvdemel = mean(vdemel)
bys year: egen mvdemlib = mean(vdemlib)
bys year: egen mvdemlibo = mean(vdemlibo)
bys year: egen mvlibdem = mean(v2x_libdem)
bys year: egen mpolbkd =mean(polbkd) 
bys year: egen mfhpolbkd =mean(fhpolbkd)
bys year: egen mfhpol1bkd =mean(fhpolbkd)
bys year: egen mvdemelbkd =mean(vdemelbkd) 
xtset ccode year
gen lvdemlib = l.vdemlib
gen lvdemlibo = l.vdemlibo
bys year: egen mvdemlibbkd =mean(vdemlibbkd) 
xtset ccode year
gen lvdemdem=l.vdemdem
bys year: egen mvdemdembkd =mean(vdemdembkd)
bys year: egen mlbdsig = mean(vdemlibbkdsig)
xtset ccode year
*gen lvdemlibo = l.vdemlibo
*xtset ccode year
*gen lvdemdemo = l.vdemdemo
bys year: egen mvdemlibbkdo =mean(vdemlibbkdo) 
*bys year: egen mvdemdembkdo =mean(vdemdembkdo) 

*constructing 5-year centered moving averages
xtset ccode year
gen lmpolbkd = l.mpolbkd
gen l2mpolbkd = l2.mpolbkd
gen fmpolbkd = f.mpolbkd
gen f2mpolbkd = f2.mpolbkd
egen mpolbkdma = rowmean(l2mpolbkd lmpolbkd mpolbkd fmpolbkd f2mpolbkd)
*replace mpolbkdma=. if year==2016|year==2017
replace mpolbkdma=. if year==2017|year==2018
gen ldmpolbkd=lmpolbkd-l2mpolbkd

xtset ccode year
gen lmfhpolbkd = l.mfhpolbkd
gen l2mfhpolbkd = l2.mfhpolbkd
gen fmfhpolbkd = f.mfhpolbkd
gen f2mfhpolbkd = f2.mfhpolbkd
egen mfhpolbkdma = rowmean(l2mfhpolbkd lmfhpolbkd mfhpolbkd fmfhpolbkd f2mfhpolbkd)

gen ldmfhpolbkd=lmfhpolbkd-l2mfhpolbkd

xtset ccode year
gen lmfhpol1bkd = l.mfhpol1bkd
gen l2mfhpol1bkd = l2.mfhpol1bkd
gen fmfhpol1bkd = f.mfhpol1bkd
gen f2mfhpol1bkd = f2.mfhpol1bkd
egen mfhpol1bkdma = rowmean(l2mfhpol1bkd lmfhpol1bkd mfhpol1bkd fmfhpol1bkd f2mfhpol1bkd)

xtset ccode year
gen lmvdemelbkd = l.mvdemelbkd
gen l2mvdemelbkd = l2.mvdemelbkd
gen fmvdemelbkd = f.mvdemelbkd
gen f2mvdemelbkd = f2.mvdemelbkd
egen mvdemelbkdma = rowmean(l2mvdemelbkd lmvdemelbkd mvdemelbkd fmvdemelbkd f2mvdemelbkd)
*replace mvdemelbkdma=. if year==2017
replace mvdemelbkdma=. if year==2018

xtset ccode year
gen lmvdemlibbkd = l.mvdemlibbkd
gen l2mvdemlibbkd = l2.mvdemlibbkd
gen fmvdemlibbkd = f.mvdemlibbkd
gen f2mvdemlibbkd = f2.mvdemlibbkd
egen mvdemlibbkdma = rowmean(l2mvdemlibbkd lmvdemlibbkd mvdemlibbkd fmvdemlibbkd f2mvdemlibbkd)
*replace mvdemlibbkdma=. if year==2017
replace mvdemlibbkdma=. if year==2018

gen ldmvdemlibbkd=lmvdemlibbkd-l2mvdemlibbkd

xtset ccode year
gen lmlbdsig = l.mlbdsig
gen l2mlbdsig = l2.mlbdsig
gen fmlbdsig = f.mlbdsig
gen f2mlbdsig = f2.mlbdsig
egen mmlbdsigma = rowmean(l2mlbdsig lmlbdsig mlbdsig fmlbdsig f2mlbdsig)

xtset ccode year
gen lmvdemlibbkdo = l.mvdemlibbkdo
gen l2mvdemlibbkdo = l2.mvdemlibbkdo
gen fmvdemlibbkdo = f.mvdemlibbkdo
gen f2mvdemlibbkdo = f2.mvdemlibbkdo
egen mvdemlibbkdoma = rowmean(l2mvdemlibbkdo lmvdemlibbkdo mvdemlibbkdo fmvdemlibbkdo f2mvdemlibbkdo)
*replace mvdemlibbkdoma=. if year==2017
replace mvdemlibbkdoma=. if year==2018

xtset ccode year
gen lmvdemdembkd = l.mvdemdembkd
gen l2mvdemdembkd = l2.mvdemdembkd
gen fmvdemdembkd = f.mvdemdembkd
gen f2mvdemdembkd = f2.mvdemdembkd
egen mvdemdembkdma = rowmean(l2mvdemdembkd lmvdemdembkd mvdemdembkd fmvdemdembkd f2mvdemdembkd)
*replace mvdemdembkdma=. if year==2017
replace mvdemdembkdma=. if year==2018

gen ldmvdemdembkd=lmvdemdembkd-l2mvdemdembkd

xtset ccode year
gen lmpolw = l.mpolitydwrse
gen l2mpolw = l2.mpolitydwrse
gen fmpolw = f.mpolitydwrse
gen f2mpolw = f2.mpolitydwrse
egen mpolwma = rowmean(l2mpolw lmpolw mpolitydwrse fmpolw f2mpolw)
replace mpolwma=. if year==2017|year==2018
*replace mpolwma=. if year==2016|year==2017

xtset ccode year
gen lmfhpolw = l.mfhfpolwrse
gen l2mfhpolw = l2.mfhfpolwrse
gen fmfhpolw = f.mfhfpolwrse
gen f2mfhpolw = f2.mfhfpolwrse
egen mfhpolwma = rowmean(l2mfhpolw lmfhpolw mfhfpolwrse fmfhpolw f2mfhpolw)

xtset ccode year
gen lmfhcivw = l.mfhfcivwrse
gen l2mfhcivw = l2.mfhfcivwrse
gen fmfhcivw = f.mfhfcivwrse
gen f2mfhcivw = f2.mfhfcivwrse
egen mfhcivwma = rowmean(l2mfhcivw lmfhcivw mfhfcivwrse fmfhcivw f2mfhcivw)
xtset ccode year
drop if year<1800

xtset ccode year
gen llibwrse = l.mvdemlibwrse
gen l2libwrse = l2.mvdemlibwrse
gen flibwrse = f.mvdemlibwrse
gen f2libwrse = f2.mvdemlibwrse
egen mlibwrse = rowmean(l2libwrse llibwrse mvdemlibwrse flibwrse f2libwrse)

xtset ccode year
gen lelwrse = l.mvdemelwrse
gen l2elwrse = l2.mvdemelwrse
gen felwrse = f.mvdemelwrse
gen f2elwrse = f2.mvdemelwrse
egen melwrse = rowmean(l2elwrse lelwrse mvdemelwrse felwrse f2elwrse)

bys year: egen mpold =mean(polity2) if polity2>=6
bys year: egen mfhpold =mean(fhpol) if fhpolfree==1
bys year: egen aaa= mean(v2x_polyarchy) if vdemel==1
bys year: egen mvdempolyd= max(aaa) 
bys year: egen mvdemldd= mean(v2x_libdem) if vdemlib==1

xtset ccode year
gen lmpold = l.mpold
gen l2mpold = l2.mpold
gen fmpold = f.mpold
gen f2mpold = f2.mpold
egen mampold = rowmean(l2mpold lmpold mpold fmpold f2mpold)

xtset ccode year
gen lmfhpold = l.mfhpold
gen l2mfhpold = l2.mfhpold
gen fmfhpold = f.mfhpold
gen f2mfhpold = f2.mfhpold
egen mamfhpold = rowmean(l2mfhpold lmfhpold mfhpold fmfhpold f2mfhpold)

xtset ccode year
gen lmvdempolyd = l.mvdempolyd
gen l2mvdempolyd = l2.mvdempolyd
gen fmvdempolyd = f.mvdempolyd
gen f2mvdempolyd = f2.mvdempolyd
egen mamvdempolyd = rowmean(l2mvdempolyd lmvdempolyd mvdempolyd fmvdempolyd f2mvdempolyd)

xtset ccode year
gen lmvdemldd = l.mvdemldd
gen l2mvdemldd = l2.mvdemldd
gen fmvdemldd = f.mvdemldd
gen f2mvdemldd = f2.mvdemldd
egen mamvdemldd = rowmean(l2mvdemldd lmvdemldd mvdemldd fmvdemldd f2mvdemldd)

*Adjust neighbor variables to = 0 when no country within 24 miles: New Zealand, Mauritius, Fiji, Australia pre-1975: just 
*Indonesia and PNG = colonies. Plus a lot of islands.
replace neighpol2=0 if ccode==920
replace neighlied=0 if ccode==920
replace neighvdem2=0 if ccode==920
replace neighvdem2l=0 if ccode==920
replace neighfhpol=0 if ccode==920

replace neighpol2=0 if ccode==590
replace neighlied=0 if ccode==590
replace neighvdem2=0 if ccode==590
replace neighvdem2l=0 if ccode==590
replace neighfhpol=0 if ccode==590

replace neighpol2=0 if ccode==950
replace neighlied=0 if ccode==950
replace neighvdem2=0 if ccode==950
replace neighvdem2l=0 if ccode==950
replace neighfhpol=0 if ccode==950

replace neighpol2=0 if ccode==900 & year<1975
replace neighlied=0 if ccode==900 & year<1975
replace neighvdem2=0 if ccode==900 & year<1975
replace neighvdem2l=0 if ccode==900 & year<1975
replace neighfhpol=0 if ccode==900 & year<1975

replace neighpol2=0 if ccode==40
replace neighlied=0 if ccode==40
replace neighvdem2=0 if ccode==40
replace neighvdem2l=0 if ccode==40
replace neighfhpol=0 if ccode==40

replace neighpol2=0 if ccode==51
replace neighlied=0 if ccode==51
replace neighvdem2=0 if ccode==51
replace neighvdem2l=0 if ccode==51
replace neighfhpol=0 if ccode==51

replace neighpol2=0 if ccode==53
replace neighlied=0 if ccode==53
replace neighvdem2=0 if ccode==53
replace neighvdem2l=0 if ccode==53
replace neighfhpol=0 if ccode==53

replace neighpol2=0 if ccode==338
replace neighlied=0 if ccode==338
replace neighvdem2=0 if ccode==338
replace neighvdem2l=0 if ccode==338
replace neighfhpol=0 if ccode==338

replace neighpol2=0 if ccode==395
replace neighlied=0 if ccode==395
replace neighvdem2=0 if ccode==395
replace neighvdem2l=0 if ccode==395
replace neighfhpol=0 if ccode==395

replace neighpol2=0 if ccode==402
replace neighlied=0 if ccode==402
replace neighvdem2=0 if ccode==402
replace neighvdem2l=0 if ccode==402
replace neighfhpol=0 if ccode==402

replace neighpol2=0 if ccode==403
replace neighlied=0 if ccode==403
replace neighvdem2=0 if ccode==403
replace neighvdem2l=0 if ccode==403
replace neighfhpol=0 if ccode==403

replace neighpol2=0 if ccode==580
replace neighlied=0 if ccode==580
replace neighvdem2=0 if ccode==580
replace neighvdem2l=0 if ccode==580
replace neighfhpol=0 if ccode==580

replace neighpol2=0 if ccode==581
replace neighlied=0 if ccode==581
replace neighvdem2=0 if ccode==581
replace neighvdem2l=0 if ccode==581
replace neighfhpol=0 if ccode==581

replace neighpol2=0 if ccode==591
replace neighlied=0 if ccode==591
replace neighvdem2=0 if ccode==591
replace neighvdem2l=0 if ccode==591
replace neighfhpol=0 if ccode==591

replace neighpol2=0 if ccode==935
replace neighlied=0 if ccode==935
replace neighvdem2=0 if ccode==935
replace neighvdem2l=0 if ccode==935
replace neighfhpol=0 if ccode==935

replace neighpol2=0 if ccode==781
replace neighlied=0 if ccode==781
replace neighvdem2=0 if ccode==781
replace neighvdem2l=0 if ccode==781
replace neighfhpol=0 if ccode==781

xtset ccode year
gen lmadgrowthf = l.madgrowthf

gen le_pt_coup = l.e_pt_coup 
gen le_pt_coup1 = le_pt_coup
replace le_pt_coup1 = 1 if le_pt_coup==2

gen le_miinteco = l.e_miinteco 

gen le_coups = l.e_coups
gen le_coups1 = le_coups
replace le_coups1 = 1 if le_coups==2
sort ccode year
bys ccode (year): gen pastcoupspipe = sum(e_coups)
sort ccode year
bys ccode (year): gen pastcoupspt = sum(e_pt_coup)
xtset ccode year
gen lpastcoupspipe=l.pastcoupspipe
gen lpastcoupspt = l.pastcoupspt
gen lpastcoupsboth = lpastcoupspipe
gen pastcoup1 =0
replace pastcoup1=1 if lpastcoupspipe>=1&lpastcoupspipe~=.

save "breakdown cases.dta", replace

*************************************
*preparing data for survival models
*************************************

*average level of democracy in other states

bysort year: egen sumpol2m = total(pol2more5)
bysort year: egen numpol2m = count(pol2more5)
gen otherpoldem = (sumpol2m-pol2more5)/(numpol2m-1) if pol2more5~=.
label var otherpoldem "proportion of pol2 dems among other states"

bysort year: egen sumvdemdem = total(vdemdem)
bysort year: egen numvdemdem = count(vdemdem)
gen othervdemdem = (sumvdemdem-vdemdem)/(numvdemdem-1) if vdemdem~=.
label var othervdemdem "proportion of other countries that are VDEM democracies"

bysort year: egen sumvdemlib = total(vdemlib)
bysort year: egen numvdemlib = count(vdemlib)
gen othervdemlib = (sumvdemlib-vdemlib)/(numvdemlib-1) if vdemlib~=.
label var othervdemlib "proportion of other countries that are VDEM liberal democracies"

bysort year: egen sumfh = total(fhpolfree)
bysort year: egen numfh = count(fhpolfree)
gen otherfhfree = (sumfh-fhpolfree)/(numfh-1) if fhpolfree~=.
label var otherfhfree "proportion of FH free states among other states"

bysort year: egen sumlied6 = total(lied6)
bysort year: egen numlied6 = count(lied6)
gen otherlied6 = (sumlied6-lied6)/(numlied6-1) if lied6~=.
label var otherlied6 "proportion of LIED dems among other states"

bysort year: egen sumpol2 = total(polity2)
bysort year: egen numpol2 = count(polity2)
gen avotherpol2 = (sumpol2-polity2)/(numpol2-1) if polity2~=.
label var avotherpol2 "average polity2 in other countries"

save "breakdown cases.dta", replace

*democratic spells
xtset ccode year
bys ccode: gen demspell = sum(polbkd)
replace demspell = . if polity2<6|polity2==.
gen dcount=.
replace dcount=1 if demspell~=.
sort ccode demspell year
by ccode demspell: gen demyrs = sum(dcount) 
replace demyrs=. if dcount==.
xtset ccode year
replace demyrs=l.demyrs+1 if polbkd==1
drop dcount
label var demyrs "years consecutively democratic (Polity2>=6)"

xtset ccode year
gen dd=0
replace dd=1 if year==1972&fhpolfree==1
replace dd=1 if fhpolfree==1&l.fhpolfree==0
bys ccode: gen dspells = sum(dd)
replace dspells=. if fhpolfree~=1
gen one=1
sort ccode dspells year
bys ccode dspells: gen fhfyrs=sum(one)
replace fhfyrs=. if fhpolfree~=1
xtset ccode year
replace fhfyrs=l.fhfyrs+1 if fhfbkd==1
label var fhfyrs "years consecutively free (FH = 'free')"
drop one dd
save "breakdown cases.dta", replace

xtset ccode year
gen vdd=vdemel+vdemlib
bys ccode: gen vbdemdemspell = sum(vdemdembkd)
replace vbdemdemspell = . if vdd==0|vdd==.
gen dcount=.
replace dcount=1 if vbdemdemspell~=.
sort ccode vbdemdemspell year
by ccode vbdemdemspell: gen vdemdemyrs = sum(dcount) 
replace vdemdemyrs=. if dcount==.
drop dcount
label var vdemdemyrs "years consecutively democratic (vdemel=1 or vdemlib=1)"
xtset ccode year
replace vdemdemyrs=l.vdemdemyrs+1 if vdemdembkd==1

xtset ccode year
bys ccode: gen vbdemlibspell = sum(vdemlibbkd)
replace vbdemlibspell = . if vdemlib==0|vdemlib==.
gen dcount=.
replace dcount=1 if vbdemlibspell~=.
sort ccode vbdemlibspell year
by ccode vbdemlibspell: gen vdemlibyrs = sum(dcount) 
replace vdemlibyrs=. if dcount==.
drop dcount
label var vdemlibyrs "years consecutively vdemlib=1"
xtset ccode year
replace vdemlibyrs=l.vdemlibyrs+1 if vdemlibbkd==1

gen lv2x_polyarchy = l.v2x_polyarchy
gen lv2x_libdem = l.v2x_libdem
gen lmadgrowth=l.madgrowth

*autocratization
xtset ccode year
bys ccode: gen llautspell = sum(autfirst)+1
replace llautspell = . if v2x_polyarchy==.
replace llautspell=. if autfirst~=1&autocratization==1
xtset ccode year
replace llautspell=l.llautspell if autfirst==1
gen dcount=.
replace dcount=1 if llautspell~=.
sort ccode llautspell year
by ccode llautspell: gen llautyrs = sum(dcount) 
replace llautyrs=. if llautspell==.
xtset ccode year
replace llautyrs=l.llautyrs+1 if autfirst==1
sort ccode llautspell year
replace dcount=. if vdemdem~=1
by ccode llautspell: gen llautyrsd = sum(dcount)-1
replace llautyrsd=. if llautspell==.
xtset ccode year
replace llautyrsd=l.llautyrsd+1 if autfirst==1
drop dcount

bys year: egen mvdemaut =mean(autfirst)
xtset ccode year
gen ldmvdemaut=l.mvdemaut-l2.mvdemaut

label var llautyrs "consecutive years before an llaut episode"

xtset ccode year
bys ccode: gen liedspell = sum(liedbkd)
replace liedspell = . if lied6==0|lied6==.
gen dcount=.
replace dcount=1 if liedspell~=.
sort ccode liedspell year
bys ccode liedspell: gen liedyrs = sum(dcount) 
replace liedyrs=. if dcount==.
drop dcount
label var liedyrs "years consecutively democratic (LIED==6)"
xtset ccode year
replace liedyrs=l.liedyrs+1 if liedbkd==1
gen lnlied=ln(liedyrs+1)
gen llnlied = l.lnlied
gen lliedyrs=l.liedyrs
save "breakdown cases.dta", replace

*past democratic breakdowns
sort ccode year
bys ccode: gen pastpolbds = sum(polbkd)
label var pastpolbds "past polity2 breakdowns from >=6 to <6" 
sort ccode year
bys ccode: gen pastvlibbkds = sum(vdemlibbkd)
label var pastvlibbkds "past vdemlib breakdowns from 1 to 0" 
sort ccode year
xtset ccode year
bys ccode: gen pastfhfbds = sum(fhfbkd)
label var pastfhfbds "past FH free breakdowns from F to PF or NF" 
xtset ccode year
bys ccode: gen pastliedbds = sum(liedbkd)
label var pastliedbds "past lied breakdowns from 6 to <6" 

xtset ccode year
bys ccode: gen pastauts = sum(autfirst)
label var pastauts "past autocratization episodes" 

xtset ccode year
gen lnlpastauts = ln(1+l.pastauts)

sort ccode year
gen llexical_index = l.lexical_index

xtset ccode year
gen lpastpolbds= l.pastpolbds
gen lnlpastpolbds=ln(lpastpolbds+1)

gen lpastfhfbds = l.pastfhfbds
gen lnlpastfhfbds=ln(lpastfhfbds+1)

gen lpastliedbkds = l.pastliedbds
gen lnlpastliedbkds=ln(lpastliedbkds+1)

xtset ccode year
gen llngdppc = l.lngdppci
label var llngdppc "lagged ln gdp per cap; Maddison"
*label var llngdppc1 "lagged ln gdp per cap; Maddison adj with WB 2019-21"

gen postcoldw = 0
replace postcoldw=1 if year>1989&year~=.
label var postcoldw "after 1989"

sort ccode year
bys ccode: gen pastvdembds = sum(vdemdembkd)
label var pastvdembds "past VDEM breakdowns from el or lib dem to not dem" 
xtset ccode year
gen lpastvdembds=l.pastvdembds
gen lnlpastvdembds =ln(lpastvdembds+1)

sort ccode year
bys ccode: gen pastvlibbds = sum(vdemlibbkd)
label var pastvlibbds "past VDEM breakdowns from lib dem to not lib dem" 
xtset ccode year
gen lpastvlibbds=l.pastvlibbds
gen lnlpastvlibbds =ln(lpastvlibbds+1)

gen d = 0
replace d=1 if demyrs>0&demyrs~=.
bys ccode: gen sumdemyrs=sum(d) 
gen lnsumdem = ln(1+sumdemyrs)
drop d

gen d = 0
replace d=1 if vdemdemyrs>0&vdemdemyrs~=.
bys ccode: gen sumvdemyrs=sum(d) 
gen lnsumvdem = ln(1+sumvdemyrs)
drop d

gen d = 0
replace d=1 if vdemlibyrs>0&vdemlibyrs~=.
bys ccode: gen sumvdemlibyrs=sum(d) 
gen lnsumvdemlib = ln(1+sumvdemlibyrs)
drop d

gen d = 0
replace d=1 if liedyrs>0&liedyrs~=.
bys ccode: gen sumliedyrs=sum(d) 
gen lnsumliedyrs = ln(1+sumliedyrs)
drop d

gen lsumvdemyrs = l.sumvdemyrs
gen lvdemdemyrs = l.vdemdemyrs
gen llnsumliedyrs=l.lnsumliedyrs
gen lnlvdemdemyrs = ln(1+lvdemdemyrs)


save "breakdown cases.dta", replace

*average growth rates in democracies and non-democracies
gen growpol2 = .
replace growpol2 = madgrowthf if pol2more5==1
gen growpol2auth =.
replace growpol2auth=madgrowthf if pol2more5==0
gen growfhf = .
replace growfhf = madgrowthf if fhpolfree==1
gen growfhfauth = .
replace growfhfauth = madgrowthf if fhpolfree==0
gen growvdem = .
replace growvdem = madgrowthf if vdemdem==1
gen growvdemauth = .
replace growvdemauth = madgrowthf if vdemdem==0
gen growlied = .
replace growlied = madgrowthf if lexical_index>=4&lexical_index~=.
gen growliedauth =.
replace growliedauth=madgrowthf if lexical_index<4

bys year: egen avgrowpol2 = mean(growpol2)
bys year: egen avgrowpol2auth = mean(growpol2auth)
bys year: egen avgrowfhf = mean(growfhf)
bys year: egen avgrowfhfauth = mean(growfhfauth)
bys year: egen avgrowvdem = mean(growvdem)
bys year: egen avgrowvdemauth = mean(growvdemauth)
bys year: egen avgrowlied = mean(growlied)
bys year: egen avgrowliedauth = mean(growliedauth)

xtset ccode year
gen lavgrowpol2 = l.avgrowpol2
gen l2avgrowpol2 = l2.avgrowpol2
gen l3avgrowpol2 = l3.avgrowpol2
gen l4avgrowpol2 = l4.avgrowpol2
gen l5avgrowpol2 = l5.avgrowpol2
gen l6avgrowpol2 = l6.avgrowpol2
gen l7avgrowpol2 = l7.avgrowpol2
gen l8avgrowpol2 = l8.avgrowpol2
gen l9avgrowpol2 = l9.avgrowpol2
gen l10avgrowpol2 = l10.avgrowpol2
egen av10growpol2 = rowmean(lavgrowpol2 l2avgrowpol2 l3avgrowpol2 l4avgrowpol2 l5avgrowpol2 l6avgrowpol2 l7avgrowpol2 l8avgrowpol2 l9avgrowpol2 l10avgrowpol2)
*use the values for US as it has the most complete year coverage of data
replace av10growpol2=. if ccode~=2
bys year: egen av10gpol2 = min(av10growpol2)
drop av10growpol2

xtset ccode year
gen lavgrowlied = l.avgrowlied
gen l2avgrowlied = l2.avgrowlied
gen l3avgrowlied = l3.avgrowlied
gen l4avgrowlied = l4.avgrowlied
gen l5avgrowlied = l5.avgrowlied
gen l6avgrowlied = l6.avgrowlied
gen l7avgrowlied = l7.avgrowlied
gen l8avgrowlied = l8.avgrowlied
gen l9avgrowlied = l9.avgrowlied
gen l10avgrowlied = l10.avgrowlied
egen av10growlied = rowmean(lavgrowlied l2avgrowlied l3avgrowlied l4avgrowlied l5avgrowlied l6avgrowlied l7avgrowlied l8avgrowlied l9avgrowlied l10avgrowlied)
*use the values for US as it has the most complete year coverage of data
replace av10growlied=. if ccode~=2
bys year: egen av10glied = min(av10growlied)
drop av10growlied

xtset ccode year
gen lavgrowvdem = l.avgrowvdem
gen l2avgrowvdem = l2.avgrowvdem
gen l3avgrowvdem = l3.avgrowvdem
gen l4avgrowvdem = l4.avgrowvdem
gen l5avgrowvdem = l5.avgrowvdem
gen l6avgrowvdem = l6.avgrowvdem
gen l7avgrowvdem = l7.avgrowvdem
gen l8avgrowvdem = l8.avgrowvdem
gen l9avgrowvdem = l9.avgrowvdem
gen l10avgrowvdem = l10.avgrowvdem
egen av10growvdem = rowmean(lavgrowvdem l2avgrowvdem l3avgrowvdem l4avgrowvdem l5avgrowvdem l6avgrowvdem l7avgrowvdem l8avgrowvdem l9avgrowvdem l10avgrowvdem)
*use the values for US as it has the most complete year coverage of data
replace av10growvdem=. if ccode~=2
bys year: egen av10gvdem = min(av10growvdem)
drop av10growvdem 

xtset ccode year
gen lavgrowfhf = l.avgrowfhf
gen l2avgrowfhf = l2.avgrowfhf
gen l3avgrowfhf = l3.avgrowfhf
gen l4avgrowfhf = l4.avgrowfhf
gen l5avgrowfhf = l5.avgrowfhf
gen l6avgrowfhf = l6.avgrowfhf
gen l7avgrowfhf = l7.avgrowfhf
gen l8avgrowfhf = l8.avgrowfhf
gen l9avgrowfhf = l9.avgrowfhf
gen l10avgrowfhf = l10.avgrowfhf
egen av10growfhf = rowmean(lavgrowfhf l2avgrowfhf l3avgrowfhf l4avgrowfhf l5avgrowfhf l6avgrowfhf l7avgrowfhf l8avgrowfhf l9avgrowfhf l10avgrowfhf)
*use the values for US as it has the most complete year coverage of data
replace av10growfhf=. if ccode~=2
bys year: egen av10gfhf = min(av10growfhf)
drop av10growfhf

xtset ccode year
gen lavgrowpol2auth = l.avgrowpol2auth
gen l2avgrowpol2auth = l2.avgrowpol2auth
gen l3avgrowpol2auth = l3.avgrowpol2auth
gen l4avgrowpol2auth = l4.avgrowpol2auth
gen l5avgrowpol2auth = l5.avgrowpol2auth
gen l6avgrowpol2auth = l6.avgrowpol2auth
gen l7avgrowpol2auth = l7.avgrowpol2auth
gen l8avgrowpol2auth = l8.avgrowpol2auth
gen l9avgrowpol2auth = l9.avgrowpol2auth
gen l10avgrowpol2auth = l10.avgrowpol2auth
egen av10growpol2auth = rowmean(lavgrowpol2auth l2avgrowpol2auth l3avgrowpol2auth l4avgrowpol2auth l5avgrowpol2auth l6avgrowpol2auth l7avgrowpol2auth l8avgrowpol2auth l9avgrowpol2auth l10avgrowpol2auth)
*use the values for US as it has the most complete year coverage of data
replace av10growpol2auth=. if ccode~=2
bys year: egen av10gpol2auth = min(av10growpol2auth)
drop av10growpol2auth

xtset ccode year
gen lavgrowvdemauth = l.avgrowvdemauth
gen l2avgrowvdemauth = l2.avgrowvdemauth
gen l3avgrowvdemauth = l3.avgrowvdemauth
gen l4avgrowvdemauth = l4.avgrowvdemauth
gen l5avgrowvdemauth = l5.avgrowvdemauth
gen l6avgrowvdemauth = l6.avgrowvdemauth
gen l7avgrowvdemauth = l7.avgrowvdemauth
gen l8avgrowvdemauth = l8.avgrowvdemauth
gen l9avgrowvdemauth = l9.avgrowvdemauth
gen l10avgrowvdemauth = l10.avgrowvdemauth
egen av10growvdemauth = rowmean(lavgrowvdemauth l2avgrowvdemauth l3avgrowvdemauth l4avgrowvdemauth l5avgrowvdemauth l6avgrowvdemauth l7avgrowvdemauth l8avgrowvdemauth l9avgrowvdemauth l10avgrowvdemauth)
*use the values for US as it has the most complete year coverage of data
replace av10growvdemauth=. if ccode~=2
bys year: egen av10gvdemauth = min(av10growvdemauth)
drop av10growvdemauth

xtset ccode year
gen lavgrowfhfauth = l.avgrowfhfauth
gen l2avgrowfhfauth = l2.avgrowfhfauth
gen l3avgrowfhfauth = l3.avgrowfhfauth
gen l4avgrowfhfauth = l4.avgrowfhfauth
gen l5avgrowfhfauth = l5.avgrowfhfauth
gen l6avgrowfhfauth = l6.avgrowfhfauth
gen l7avgrowfhfauth = l7.avgrowfhfauth
gen l8avgrowfhfauth = l8.avgrowfhfauth
gen l9avgrowfhfauth = l9.avgrowfhfauth
gen l10avgrowfhfauth = l10.avgrowfhfauth
egen av10growfhfauth = rowmean(lavgrowfhfauth l2avgrowfhfauth l3avgrowfhfauth l4avgrowfhfauth l5avgrowfhfauth l6avgrowfhfauth l7avgrowfhfauth l8avgrowfhfauth l9avgrowfhfauth l10avgrowfhfauth)
*use the values for US as it has the most complete year coverage of data
replace av10growfhfauth=. if ccode~=2
bys year: egen av10gfhfauth = min(av10growfhfauth)
drop av10growfhfauth

xtset ccode year
gen lavgrowliedauth = l.avgrowliedauth
gen l2avgrowliedauth = l2.avgrowliedauth
gen l3avgrowliedauth = l3.avgrowliedauth
gen l4avgrowliedauth = l4.avgrowliedauth
gen l5avgrowliedauth = l5.avgrowliedauth
gen l6avgrowliedauth = l6.avgrowliedauth
gen l7avgrowliedauth = l7.avgrowliedauth
gen l8avgrowliedauth = l8.avgrowliedauth
gen l9avgrowliedauth = l9.avgrowliedauth
gen l10avgrowliedauth = l10.avgrowliedauth
egen av10growliedauth = rowmean(lavgrowliedauth l2avgrowliedauth l3avgrowliedauth l4avgrowliedauth l5avgrowliedauth l6avgrowliedauth l7avgrowliedauth l8avgrowliedauth l9avgrowliedauth l10avgrowliedauth)
*use the values for US as it has the most complete year coverage of data
replace av10growliedauth=. if ccode~=2
bys year: egen av10gliedauth = min(av10growliedauth)
drop av10growliedauth

gen growdifpol = av10gpol2 - av10gpol2auth
gen growdiffhf = av10gfhf - av10gfhfauth
gen growdifvdem  = av10gvdem - av10gvdemauth
gen growdiflied = av10glied - av10gliedauth

replace mpol=. if year>2018
egen maxmpol2more5 = max(mpol2more5)
egen maxmfhpolfree = max(mfhpolfree)
egen maxmpol = max(mpol)
egen maxmvdempoly = max(mvdempoly)
egen maxmvdemld = max(mvdemld)
egen maxproplied6 = max(proplied6)

gen polpcmax = mpol2more5*100/maxmpol2more5
gen fhpcmax = mfhpolfree*100/maxmfhpolfree
gen lied6max = proplied6*100/maxproplied6
gen mpolmax = mpol*100/maxmpol
gen mvdempolymax = mvdempoly*100/maxmvdempoly
gen mvdemldmax = mvdemld*100/maxmvdemld

egen maxmvdemel = max(mvdemel)
egen maxmvdemlib = max(mvdemlib)
bys year: egen countvdemlib = sum(vdemlib)
gen vdemelpcmax = mvdemel*100/maxmvdemel
gen vdemlibpcmax = mvdemlib*100/maxmvdemlib

gen mfhpoldn = (7-mfhpold)/6
gen fhpolr = 7-fhpol

*Average change
xtset ccode year
gen dpolif = pol2norm-l.pol2norm if l.polity2>=6&l.polity2~=.&year<2019
bys year: egen xx = mean(dpolif)
bys year: egen mdpolif = max(xx)
drop xx
tssmooth ma mamdpolif = mdpolif, window(2 1 2)

xtset ccode year
gen dfhpolr = fhpolrn-l.fhpolrn if l.fhpolfree==1&year<2021
bys year: egen xx = mean(dfhpolr)
bys year: egen mdfhpolr = max(xx)
drop xx
tssmooth ma mamdfhpolr = mdfhpolr, window(2 1 2)

xtset ccode year
gen dvdpoly = v2x_polyarchy-l.v2x_polyarchy if l.vdemel==1&year<2021
bys year: egen xx = mean(dvdpoly)
bys year: egen mdvdpoly = max(xx)
drop xx
tssmooth ma mamdvdpoly = mdvdpoly, window(2 1 2)

xtset ccode year
gen dvdlibdem = v2x_libdem-l.v2x_libdem if l.vdemlib==1&year<2021
bys year: egen xx = mean(dvdlibdem)
bys year: egen mdvdlibdem = max(xx)
drop xx
tssmooth ma mamdvdlibdem = mdvdlibdem, window(2 1 2)

xtset ccode year
gen lmdvdlibdem= l.mdvdlibdem
gen lmdvdpoly = l.mdvdpoly

xtset ccode year
bys ccode: carryforward country, replace

xtset ccode year
gen llnsumvdem = l.lnsumvdem
gen lprespipedpi = l.prespipedpi
gen lneighvdem2 = l.neighvdem2
gen lgrowdifvdem = l.growdifvdem
gen lavginimkt = l.avginimkt
gen lavginidisp = l.avginidisp
gen lCapacity = l.Capacity
gen llnoil = l.lnoil
gen llnsumvdemlib = l.lnsumvdemlib
gen lpostcoldw = l.postcoldw
gen lneighvdem2l = l.neighvdem2l
gen llnsumdem = l.lnsumdem
gen lneighpol2 = l.neighpol2
gen lgrowdifpol = l.growdifpol
gen lneighfhpol = l.neighfhpol
gen lgrowdiffhf = l.growdiffhf
gen lneighlied = l.neighlied
gen lgrowdiflied = l.growdiflied

gen lv2cltrnslw = l.v2cltrnslw_osp
gen lv2clacjstm=l.v2clacjstm_osp
gen lv2clacjstw=l.v2clacjstw_osp

gen lsumvdemyrs2 = lsumvdemyrs^2
gen lsumvdemlib = l.sumvdemlibyrs

gsort -year
bys ccode: carryforward country, replace
xtset ccode year
save "breakdown cases.dta", replace

*now create survival analysis datasets

*Breakdowns: (a) from polity2>=6 to polity2<6, (b) from fhstat="F" to fhstat="PF" or "NF", 
*(d) from VDEM dem to notdem, (e) from LIED 6 to below 6, (f) from no autocratization to autocratization.

keep if pol2more5==1|polbkd==1
sort ccode year
gen dn=0
replace dn=1 if demyrs==1
gen demid=sum(dn)
drop dn
*counter of dem spells
sort cyear
gen x=.
*first year for country
replace x=1 if ccode~=ccode[_n-1]&pol2more5==1
*first after a non-dem year
replace x=1 if pol2more5==1&pol2more5[_n-1]~=1
gen spellno = sum(x)
replace polity2=. if year>2018
save "pol6sur.dta", replace

*use "breakdown cases.dta", clear
*keep if brosdembmr==1|bmrbkd==1
*sort ccode year
*gen dn=0
*replace dn=1 if bmrdyrs==1
*xtset ccode year
*gen demid=sum(dn)
*drop dn
*save "bmrsur.dta", replace

use "breakdown cases.dta", clear
keep if fhpolfree==1|fhpolbkd==1
xtset ccode year
replace dspells = l.dspells if fhpolbkd==1&dspells==.
gen demid=ccode*10000+dspells
*counter of dem spells
sort cyear
gen x=.
*first year for country
replace x=1 if ccode~=ccode[_n-1]&fhpolfree==1
*first after a non-dem year
replace x=1 if fhpolfree==1&fhpolfree[_n-1]~=1
gen spellno = sum(x)
save "fhsur.dta", replace

use "breakdown cases.dta", clear
keep if vdemdem==1|vdemdembkd==1
sort ccode year
gen dn=0
replace dn=1 if vdemdemyrs==1
xtset ccode year
gen demid=sum(dn)
drop dn
*counter of dem spells
sort cyear
gen x=.
*first year for country
replace x=1 if ccode~=ccode[_n-1]&vdemdem==1
*first after a non-dem year
replace x=1 if vdemdem==1&vdemdem[_n-1]~=1
gen spellno = sum(x)
save "vdemsur.dta", replace

use "breakdown cases.dta", clear
xtset ccode year
replace vdemlibyrs=l.vdemlibyrs+1 if vdemlibbkd==1
keep if vdemlib==1|vdemlibbkd==1
sort ccode year
gen dn=0
replace dn=1 if vdemlibyrs==1
xtset ccode year
gen demid=sum(dn)
drop dn
*counter of dem spells
sort cyear
gen x=.
*first year for country
replace x=1 if ccode~=ccode[_n-1]&vdemlib==1
*first after a non-dem year
replace x=1 if vdemlib==1&vdemlib[_n-1]~=1
gen spellno = sum(x)
save "vlibsur.dta", replace

use "breakdown cases.dta", clear
keep if lied6==1|liedbkd==1
sort ccode year
gen dn=0
replace dn=1 if liedyrs==1
gen demid=sum(dn)
drop dn
*counter of dem spells
sort cyear
gen x=.
*first year for country
replace x=1 if ccode~=ccode[_n-1]&lexical_index==6
*first after a non-dem year
replace x=1 if lexical_index==6&(lexical_index[_n-1]<6|lexical_index[_n-1]==.)
gen spellno = sum(x)
save "liedsur.dta", replace

*Autocratization episodes

use "breakdown cases.dta", clear
xtset ccode year
gen lv2xregime=l.v2x_regime
keep if (autocratization==0|autfirst==1)&(lv2xregime==2|lv2xregime==3|v2x_regime==2|v2x_regime==3)
gen x=.
*first year for country
replace x=1 if ccode~=ccode[_n-1]&autocratization==0
*first after a non-llaut year
replace x=1 if autocratization==0&l.autocratization~=0
gen spellno = sum(x)
gen demid = spellno
save "llautsur.dta", replace


